有点小问题。我试图使用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×H×D): </span>60 × 152,4 × 64 cm</div>
以上内容在$html
中和下面的代码:
$table_html = $html;
preg_match_all("'Dimensions of the product (W×H×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×H×D'): </span>(.*?)</div>'si",$table_html,$extract);
var_dump($extract);
您需要使用反斜杠'
转义W×H×D
周围的括号。
谢尔盖注意到,你也有一个</td>
在你的模式,它应该被替换为一个空格匹配你的HTML字符串。