【问题标题】:preg_match() not working in some casespreg_match() 在某些情况下不起作用
【发布时间】:2013-05-19 20:17:14
【问题描述】:

我觉得这应该是一个如此简单的“更改逗号”,所以我已经完成了我的研究并尝试了许多不同的方法,但似乎没有任何效果。首先是我用来调试它的代码:

/* 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 上使用正则表达式的third time。请请不要。否则很容易。
  • @likeitlikeit ,我尝试向比我更有经验的人学习,所以我会按照 Spudley 的建议检查 PHP 的 DOMDocument 类
  • 对不起,如果我过来让我失望了。继续检查 DOMDocument 和 XPath。它们确实是执行您想做的事情的正确工具。

标签: php regex preg-match


【解决方案1】:

您遇到的最大问题是您尝试使用正则表达式解析 HTML 代码。即使您可以让它与您拥有的数据一起工作,只要数据包含嵌套的&lt;ul&gt; 标签,您的正则表达式就会爆炸,此时让它工作变得非常困难。解析 HTML 确实应该使用 DOM 解析器(即 PHP 的 DOMDocument 类)来完成。正则表达式不适合这项工作。

也就是说,如果您必须使用正则表达式,则需要使用s 修饰符,因为输入跨越多行。此修饰符更改正则表达式中点字符的行为,使其包含换行符。

所以你的最终模式需要如下所示:

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

希望对您有所帮助。

【讨论】:

  • 既然你提到了这个,我记得很久以前读过一次类似的东西,问题是&lt;ul&gt;&lt;ul&gt;&lt;/ul&gt;&lt;/ul&gt;被识别为只有1个ul标签。我会检查 DOMDocument,同时,你完美地回答了我的问题。
【解决方案2】:

您在第二种情况下的正则表达式是多行的。将“m”附加到您的函数调用中:

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

【讨论】:

  • 真的很相似,只是加了's'而不是'm'。
【解决方案3】:

我使用了一些修改一些 SO 答案的代码;但是我通过检查其他一些答案并查看 Patrice Levesque 的答案找到了解决方案。我用's'来调用函数,根据this question

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

【讨论】:

  • 啊,看起来你在我打字的时候找到了答案。但我会留下我的答案,因为 DOM 解析器与正则表达式点是需要牢记的重要一点。
  • 我接受了你的,因为在 1 分钟内你的和我的有所不同,你的似乎是合法的 (;
猜你喜欢
  • 2018-11-28
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-17
  • 2017-06-29
  • 1970-01-01
  • 2019-07-11
相关资源
最近更新 更多