preg_match 在 html 代码中有换行符时不起作用

preg_match doesn't work when there is a newline in html code

我正在使用 preg_match_all 来匹配也在 <strong> 标签之间的 <td> 内的文本。但是我有一个问题,html 代码中有换行符;这是 html:

<td 
class="vcenter text-center">
<strong>Match This </strong></td>

现在我使用这个模式来获取文本:

!<td\nclass="vcenter text-center">\n<strong>(.*?)<\/strong><\/td>!

这确实获取了文本,但如果换行符(在 td 标记中)从 html 代码中消失,它将不起作用。在这种情况下我能做什么?

P.S:我正在使用 curl 来获取 html(而且我不想添加额外的 class,例如 simple_html_dom :-s).

谢谢!

您不应使用正则表达式来解析 html,而应使用 xml parser.

但就换行而言:您需要 1 个或多个空格,而不是专门的换行。

您可以将 \n 替换为 \s+ 来实现:

!<td\s+class="vcenter text-center">\n<strong>(.*?)<\/strong><\/td>!