PHP正则表达式和输入问题

发布于 2024-12-07 03:11:26 字数 540 浏览 3 评论 0原文

代码


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

问题


如果我使用 Enter 为 $string 分配任何值,我的正则表达式将不起作用。

示例:

//This doesn't work
$string = '[level-0-]This is a 
test[/level]';
//This works
$string = '[level-0-]This is a test[/level]';

我想要什么


我希望我的正则表达式能够工作,无论之间有什么字符(输入等)。

如果有人能帮助我解决这个问题,我会很高兴。我还没有深入研究正则表达式,所以我不太擅长它:(

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 = '[level-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 :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

阳光的暖冬 2024-12-14 03:11:26

您只需要在正则表达式中添加 DOTALL 标志 /s 即可。这允许点 . 匹配任何字符,包括换行符(默认情况下不这样做)。

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

另请参阅 PCRE 标志列表 http://php.net/manual/en /reference.pcre.pattern.modifiers.php

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

り繁华旳梦境 2024-12-14 03:11:26

您可能需要通过将 /s 标志添加到您的模式中,让正则表达式将其输入视为单行。

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

十年不长 2024-12-14 03:11:26

使用 s 模式修饰符:

如果设置此修饰符,模式中的点元字符将匹配所有字符,包括换行符。如果没有它,换行符将被排除。 (参考:正则表达式模式中可能的修饰符

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)

决绝 2024-12-14 03:11:26

这应该有效:

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

this should work :

(preg_match_all("#\[level-(.+)-\](.+?)(\b)*(.+?)*\[/level\]#", $string, $matches)
北音执念 2024-12-14 03:11:26

“Test[.|\n]is[.|\n]a[.|\n]test” 这可能有效

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文