Preg_match()在某些情况下不起作用


preg_match() not working in some cases

我觉得这应该是一个很简单的'更改逗号',所以我做了我的研究,并尝试了许多不同的东西,但似乎没有工作。首先是我用来调试它的代码:

/* More code before */
$Test = "This is a test <ul>TEST</ul> Blabla";
$Real = $Data['chapters']['introduction'];
var_dump($Real);
echo "'n'n";
preg_match('/<ul>(.*)<'/ul>/', $Test, $VarTest);
var_dump($VarTest);
echo "'n'n";
preg_match('/<ul>(.*)<'/ul>/', $Real, $VarReal);
var_dump($VarReal);

结果如下:

string(1888) "<p>The <b>theory of relativity</b>, or simply <b>relativity</b>, generally encompasses two theories of <a href="http://en.wikipedia.org/wiki/Albert_Einstein" title="Albert Einstein">Albert Einstein</a>: <a href="http://en.wikipedia.org/wiki/Special_relativity" title="Special relativity">special relativity</a> and <a href="http://en.wikipedia.org/wiki/General_relativity" title="General relativity">general relativity</a>. Concepts introduced by the theories of relativity include:</p>
<ul>
  <li>
    <p>Measurements of various quantities are <i>relative</i> to the velocities of observers. In particular, space and time can <a href="http://en.wikipedia.org/wiki/Time_dilation" title="Time dilation">dilate</a>.</p>
  </li>
  <li>
    <p><a href="http://en.wikipedia.org/wiki/Spacetime" title="Spacetime">Spacetime</a>: space and time should be considered together and in relation to each other.</p>
  </li>
  <li>
    <p>The speed of light is nonetheless invariant, the same for all observers.</p>
  </li>
</ul>
<p>The term &quot;theory of relativity&quot; was based on the expression &quot;relative theory&quot; (<a href="http://en.wikipedia.org/wiki/German_language" title="German language">German</a>: <span lang="de"><i>Relativtheorie</i></span>) used by <a href="http://en.wikipedia.org/wiki/Max_Planck" title="Max Planck">Max Planck</a> in 1906, who emphasized how the theory uses the <a href="http://en.wikipedia.org/wiki/Principle_of_relativity" title="Principle of relativity">principle of relativity</a>. In the discussion section of the same paper <a href="http://en.wikipedia.org/wiki/Alfred_Bucherer" title="Alfred Bucherer">Alfred Bucherer</a> used for the first time the expression &quot;theory of relativity&quot; (<a href="http://en.wikipedia.org/wiki/German_language" title="German language">German</a>: <span lang="de"><i>Relativit&auml;tstheorie</i></span>).</p>
"
array(2) {
  [0]=>
  string(13) "<ul>TEST</ul>"
  [1]=>
  string(4) "TEST"
}

array(0) {
}

任何想法为什么最后一个数组是空的(当它应该包含3个列表元素)?

更多信息,它是使用PDO从MySQL检索的,我尝试过转义它(对于引号),替换引号,检查此文本大小是否低于preg_match()字符串限制,我只是找不到问题所在。我认为代码本身说明了具体的问题在哪里,无论如何,我很乐意执行您需要的测试。谢谢。

您在这里遇到的最大问题是您试图使用正则表达式解析HTML代码。即使您可以让它与您拥有的数据一起工作,一旦数据包含嵌套的<ul>标记,您的regex就会爆炸,并且在这一点上使它工作将变得极其困难。解析HTML实际上应该使用DOM解析器(即PHP的DOMDocument类)来完成。Regex是错误的工具。

也就是说,如果必须使用regex,则需要使用s修饰符,因为输入跨越多行。这个修饰符改变正则表达式中点字符的行为,使其包括换行字符。 所以你最终的样式应该是这样的:
preg_match('/<ul>(.*)<'/ul>/s', $Real, $VarReal);

希望对你有帮助。

第二种情况下的正则表达式是多行的。在函数调用后添加" m ":

preg_match('/<ul>(.*)<'/ul>/m', $Real, $VarReal);

我使用了我从修改一些SO答案一点点的代码;但我通过查看其他答案和帕特里斯·莱韦斯克的答案找到了答案。我使用's'来调用函数,根据这个问题:

preg_match('/<ul>(.*)<'/ul>/s', $Real, $VarReal);