简体   繁体   English

PHP中的多行正则表达式问题

[英]Multiline regex issue in PHP

Consider the code below, why it is not working? 考虑下面的代码,为什么它不起作用?

<?php

$str = "
<h4>
   title
</h4>
";

$result = preg_match_all ('/<h4>([\d\D])<\/h4>/mi', $str, $matches);
var_dump($matches);

You probably meant 你可能意味着

$str = "
<h4>
   title
</h4>
";

$result = preg_match_all ('/<h4>(.+?)<\/h4>/si', $str, $matches);
var_dump($matches);

The regex you applied, '/<h4>([\\d\\D])<\\/h4>/mi' , means "Match an opening h4, one character that's either a digit or not a digit, and a closing h4." 您应用的正则表达式'/<h4>([\\d\\D])<\\/h4>/mi'意思是“匹配一个开头h4, 一个数字或非数字字符和一个结尾h4。 “ But you have plenty of characters to match, so you need to specify a quantifier ("more than one", + ). 但是您要匹配的字符很多,因此需要指定一个量词(“多个”, + )。 Update: you need a non-greedy quantifier, +? 更新:您需要一个非贪婪的量词, +? , if you have more than one h4 (very likely!) And the class [\\d\\D] can be reduced to "any character", . ,如果您有多个h4(很有可能!),并且类[\\d\\D]可以简化为“任何字符”, . . One more point: you need to use /s instead of /m to get the behaviour you want. 还有一点:您需要使用/s而不是/m来获得所需的行为。

This will probably include the newlines in your match! 这可能包括您的比赛中的换行符!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM