[英]Why isn't my simple regex working?
I have a list of steps, and they are being echo
'd into the view as li
items in a ol
. 我有一个步骤列表,它们作为
ol
li
项目被echo
到视图中。
So, I need to remove leading numbers (that look like ol { list-style: decimal }
. 因此,我需要删除前导数字(看起来像
ol { list-style: decimal }
。
Here is an example array member 这是一个数组成员示例
- Combine 1 tbs oil...
结合1汤匙油...
I tried this regex 我尝试过这个正则表达式
/^\d+\.\s*/
However, it didn't remove the 1.
in the example above 但是,它没有删除上面示例中的
1.
。
But , when I remove the start of line anchor ( ^
), it works. 但是 ,当我删除行锚(
^
)的开始时,它可以工作。
Here is the complete code. 这是完整的代码。
foreach($method as &$step) {
$step = trim(preg_replace('/^\d+\.\s*/', '', $step));
var_dump($step); // This is what I quoted above
}
What am I doing wrong? 我究竟做错了什么?
Thanks! 谢谢!
Sorry guys, here is a var_dump()
of one of the lines.. 抱歉,这是其中一行的
var_dump()
。
string(43) "4. Remove from heat and cover to keep warm."
To me, it doesn't look there is anything before the digit. 在我看来,数字前没有任何东西。
is there any whitespace before the digit? 数字前是否有空格?
Try 尝试
/^\s*\d+\.\s*/
There's probably extra whitespace around. 周围可能还有多余的空格。 Also you can get
^
to change its behavior with the m
modifier: 您也可以使用
m
修饰符来更改^
的行为:
<?php
$s = <<<EOS
1. Combine 1 tbs oil...
2. Hello World
3. Ok then!
EOS;
echo trim(preg_replace('/^\s*\d+\.\s*/m', '', $s));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.