m regex pattern modifiers

女生的网名这么多〃 提交于 2021-02-05 08:38:32

问题


$subject = "SIverygood \n SIverygood\n";
$pattern = '/^SI/m';

preg_match_all($pattern, $subject, $matches2,PREG_OFFSET_CAPTURE);
var_dump($matches2);

I am unable to understand m and s modifier. I want to get SI at the beginning of every newline. I do it for practise purpose to understand the m and s modifier. The above example just return first SI. But i want both.


回答1:


The m is a multi-line modifier which will makes the start anchor matches the start of each line instead of whole of your string. And in your string at the start of first line you have SI and it returns it but in second line the start of the line is a space not SI.

If you want to ignore the whitespaces at the leading of your line you can use following regex:

$pattern = '/^\s*(SI)/m';

And get the result of first matched group. Read more about modifiers http://www.regular-expressions.info/modifiers.html



来源:https://stackoverflow.com/questions/35771159/m-regex-pattern-modifiers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!