问题
$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