正则表达式PHP regex在特定场景中的问题


Regular Expression PHP regex issue in a specific scenario

有点小问题。我试图使用preg_match_all,但有些正则表达式不适合其中一种情况。

<tr>
    <td class="FieldTitle" valign="top">EAN code:</td>
    <td class="Field" valign="top">3838942897078</td>
</tr>

在上面的代码中是变量$html

$table_html = $html;
preg_match_all("'EAN code:</td>'s*<td class='"Field'" valign='"top'">(.*?)</td>'si",$table_html,$extract);
$ean = $extract[1][0];
return $ean;

返回3838942897078。这是正确的,但是对于不同的场景,相同的代码会给出$extract的var_dump的空数组。这意味着我没有找到任何匹配。

<div class="Field"><span class="Title">Dimensions of the product (W&#215;H&#215;D): </span>60 &#215; 152,4 &#215; 64 cm</div>

以上内容在$html

和下面的代码:

$table_html = $html;
preg_match_all("'Dimensions of the product (W&#215;H&#215;D):</span>(.*?)</div>'si",$table_html,$extract);
var_dump($extract);

这表明在转储中该数组为空。有人能解释一下这个问题吗?我已经尝试了preg_match和preg_match_all,没有运气。非常感谢你的帮助。

这个适合我:

$table_html = $html;
preg_match_all("'Dimensions of the product '(W&#215;H&#215;D'): </span>(.*?)</div>'si",$table_html,$extract);
var_dump($extract);

您需要使用反斜杠'转义W&#215;H&#215;D周围的括号。

谢尔盖注意到,你也有一个</td>在你的模式,它应该被替换为一个空格匹配你的HTML字符串。