我有这个字符串,用于简单的html解析器和类似的东西
<b>Atmosp'Hair, Caroline Michellod</b><i> in <a href="1" target="_top">Leytron</a></i>, Einzelunt., <a href="1" target="result">+++</a>, <a href="google.com" target="_blank">CHE-137.645.261</a><a href="pdf" target="_blank">Download pdf</a>
<b>Bar La Gouttière, Y. Maret</b><i> in <a href="http://www.example.com">Dolor</a><a href="2" target="_top">Martigny</a></i>, Einzelunt., <a href="2" target="result">+++</a>, <a href="yahoo.com" target="_blank">CHE-112.712.556</a><a href="http:/wwww.coocc.com">Doloo</a>
<b>Catherine Michellod</b><i> in <a href="3" target="_top">Bagnes</a></i>, Einzelunt., <a href="3" target="result">+</a>, <a href="bing.com" target="_blank">CHE-111.755.770</a><a href="pdf" target="_blank">Download pdf</a>
我需要的是从中获取新数组并在页面上显示为
<a href="google.com" target="_blank">CHE-137.645.261</a>
<a href="yahoo.com" target="_blank">CHE-112.712.556</a>
<a href="bing.com" target="_blank">CHE-111.755.770</a>
我试图找到属性_blank,但有时会有另一个链接与该属性,也试图找到NT子,但有时还有另一个标记。很多不好的HTML,唯一独特的是href内部html以 CHE
开头答案 0 :(得分:1)
只需使用preg_match_all()
即可获得所有匹配,例如
<?php
preg_match_all("/<[^>]*>CHE[^<]*<[^>]*>/", $str, $m);
print_r($m[0]);
?>
输出:
Array
(
[0] => <a href="google.com" target="_blank">CHE-137.645.261</a>
[1] => <a href="yahoo.com" target="_blank">CHE-112.712.556</a>
[2] => <a href="bing.com" target="_blank">CHE-111.755.770</a>
)