我正在尝试使用以下代码获取具有特定颜色#ff0000的两个<span...</span>
之间的数据,但我没有获得数据!谁能告诉我我做错了什么?
数据示例:
<span style="color: #ff0000;">get this text1</span> |
<span style="color: #ff0000;">get this text2</span> |
<span style="color: #ff0000;">get this text3</span> |
<span style="color: #ff0000;">get this text4</span> |
php代码:
if(preg_match_all("/<span style=\"color: #ff0000;\">(.*?)</span>/i", $code2, $epititle))
{
print_r($epititle[2]);
}
答案 0 :(得分:3)
不要使用正则表达式解析HTML。如果你这样做,一只小猫会die()
;
稳定的解决方案是使用DOM:
$doc = new DOMDocument();
$doc->loadHTML($html);
foreach($doc->getElementsByTagName('span') as $span) {
echo $span->nodeValue;
}
请注意,DOMDocument也可以优雅地解析HTML片段,如下所示:
$doc->loadHTML('<span style="color: #ff0000;">get this text1</span>');
答案 1 :(得分:2)
虽然我也建议使用DOM解析器,但这里是正则表达式的工作版本:
if(preg_match_all("%<span style=\"color: #ff0000;\">(.*?)</span>%i", $code2, $epititle))
我所做的更改:我将分隔符从/
更改为%
,因为</span>
中也使用了斜杠
完整输出(print_r($epititle);
)是:
Array
(
[0] => Array
(
[0] => <span style="color: #ff0000;">get this text1</span>
[1] => <span style="color: #ff0000;">get this text2</span>
[2] => <span style="color: #ff0000;">get this text3</span>
[3] => <span style="color: #ff0000;">get this text4</span>
)
[1] => Array
(
[0] => get this text1
[1] => get this text2
[2] => get this text3
[3] => get this text4
)
)
答案 2 :(得分:0)
$code2 = '<span style="color: #ff0000;">get this text1</span>';
preg_match_all("/<span style=\"color: #ff0000;\">(.*?)<\/span>/i", $code2, $epititle);
print_r($epititle);
输出
Array (
[0] => Array ( [0] => get this text1 )
[1] => Array ( [0] => get this text1 )
)