我想从 html 文档中的以下元标记中查找型号
<meta name="description" content="Model AB-1234. Model description here" />
我只想匹配型号(AB-1234
(。我已经尝试了几件事,我将在下面包括 2 件事:
preg_match('/<meta name="description" content="'bmodel'b(.*)"/i', $html, $model);
这个返回AB-1234. Model description here
===
preg_match('/<meta name="description" content="(.*)"/i', $html, $model);
这个返回:Model AB-1234. Model description here
可能一种方法是在.
(点(处停下来,但我不知道如何处理。
谢谢
$str = '<meta name="description" content="Model AA-1234. Model description here" />
<meta name="description" content="Model AB-1234. Model description here" />
<meta name="description" content="Model AC-1234. Model description here" />
<meta name="description" content="Model AD-1234. Model description here" />
';
preg_match_all('/content="Model (.*?)'./is', $str, $data);
if(!empty($data[1])){
$models = $data[1];
print_r($models);
}
//结果
Array ( [0] => AA-1234 [1] => AB-1234 [2] => AC-1234 [3] => AD-1234 )
preg_match('/<meta name="description" content="model's+([^.]*)"/i', $html, $model);
一般来说,最好不要使用正则表达式来解析 HTML,因为你对确切的布局非常敏感。更好的是使用 DOM 解析库。提取 content
属性,然后您可以使用正则表达式提取其中的各个部分。
你可以使用这个:
preg_match('/<meta name="description" content="model's++'K[^.]++/i',
$html, $model);
print_r($model);
解释:
/<meta name="description" content="model
's++ # one or more spaces, tabs, newlines (possessive)
'K # reset the pattern begining
[^.]++ # all that is not a dot one or more times (possessive)
有关所有格量词的更多信息
请注意,使用 DOM 提取属性内容,然后使用正则表达式查找模型更安全。例:
$html = <<<LOD
<meta name="description" content="Model AB-1234. Model description here" />
LOD;
$doc=new DOMDocument();
$doc->loadHTML($html);
$content=$doc->getElementsByTagName('meta')->item(0)->getAttribute('content');
preg_match('/model's++'K[^.]++/i', $content, $model);