开发者

PHP Regex and enter problem

Code


(preg_match_all("#\[level-(.+)-\](.+?)\[/level\]#", $string, $matches)

Problem


if I assign any value to $string with enter, my regex doesn't work.

Example:

//This doesn't work
$string = '[level-0-]This is a 
test[/level]';
//This works
$string = '[l开发者_开发问答evel-0-]This is a test[/level]';

What I Want


I would like my regex to work no matter what characters between (enter, etc..).

I will be glad if anyone could help me out with this one. I still didn't dig into regex yet so I'm not that good with it :(


You just need the DOTALL flag /s in your regex. This allows the dot . to match any character, including linebreaks (which it doesn't do per default).

preg_match_all("#\[level-(.+)-\](.+?)\[/level\]#s", ....

See also the PCRE flags list http://php.net/manual/en/reference.pcre.pattern.modifiers.php


You probably need to get regex to treat its input as a single line by adding the /s flag to your pattern.


Use s Pattern Modifier:

If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. (Ref: Possible modifiers in regex patterns)


this should work :

(preg_match_all("#\[level-(.+)-\](.+?)(\b)*(.+?)*\[/level\]#", $string, $matches)


"Test[.|\n]is[.|\n]a[.|\n]test" this might work

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜