我正在使用正则表达式从网站中提取数据,但现在我发现了一个问题。
这是我要解析的原始HTML的一部分。我想在“< a href>”后面的“descuentos-”和城市之后提取文本。
<div id="cities2_2">
<a href = "http://website.com/descuentos-espana/">Badajoz</a>
<a href = "http://website.com/descuentos-espana/">Badalona</a>
<a href = "http://website.com/descuentos-barcelona/">Barcelona</a>
<a href = "http://website.com/descuentos-bilbao/">Bilbao</a>
<a href = "http://website.com/descuentos-espana/">Burgos</a>
</div>
</div>
<div class="capa_cities" onmouseover="act_formato(3, 2);"
onmouseout="desact_formato(3, 2);">
<h2 id="title_city3_2">C</h2>
<div id="cities3_2">
<a href = "http://website.com/descuentos-espana/">Cáceres</a>
<a href = "http://website.com/descuentos-cadiz/">Cádiz</a>
<a href = "http://website.com/descuentos-espana/">Cartagena</a>
<a href = "http://website.com/descuentos-espana/">Castellón</a>
<a href = "http://website.com/descuentos-espana/">Ceuta</a>
<a href = "http://website.com/descuentos-espana/">Ciudad Real</a>
<a href = "http://website.com/descuentos-cordoba/">Córdoba</a>
<a href = "http://website.com/descuentos-espana/">Cuenca</a>
我可以查找&lt; a href =“http://website.com/descuentos-(。*)”&gt;,但还有其他一些与网站中的模式相匹配。所以我现在有了这种模式:
#<div id="cities[0-9]+_2">(<a href = "http://website.com/descuentos-(.*?)/">(.*?)</a>)*#
我想让它递归。我的意思是:对于每个“&lt; a href =”http://website.com/descuentos-(。*)/“&gt;(。*)&lt; / a&gt;”发现,搜索里面的两个小图案。
有没有办法在正则表达式中实现这一点,或者我必须通过preg_match_all重新处理它?</ p>
答案 0 :(得分:2)
选项1:快捷方式:是的,使用preg_match_all()
preg_match_all('#<a href = "http://website.com/descuentos-(.*?)/">.*?</a>#', $str, $matches);
echo "<pre>";
print_r($matches);
echo "</pre>";
返回:
Array
(
[0] => Array
(
[0] => Badajoz
[1] => Badalona
[2] => Barcelona
[3] => Bilbao
[4] => Burgos
[5] => Cáceres
[6] => Cádiz
[7] => Cartagena
[8] => Castellón
[9] => Ceuta
[10] => Ciudad Real
[11] => Córdoba
[12] => Cuenca
)
[1] => Array
(
[0] => espana
[1] => espana
[2] => barcelona
[3] => bilbao
[4] => espana
[5] => espana
[6] => cadiz
[7] => espana
[8] => espana
[9] => espana
[10] => espana
[11] => cordoba
[12] => espana
)
[2] => Array
(
[0] => Badajoz
[1] => Badalona
[2] => Barcelona
[3] => Bilbao
[4] => Burgos
[5] => Cáceres
[6] => Cádiz
[7] => Cartagena
[8] => Castellón
[9] => Ceuta
[10] => Ciudad Real
[11] => Córdoba
[12] => Cuenca
)
)
Time elapsed: 0.000104904174805
选项2: DOM解析器: ($ str是你的文字);
$dom = new DomDocument();
$dom->loadHTML($str);
$links = $dom->getElementsByTagName('a');
foreach($links as $link){
$href = $link->getAttribute('href');
echo $href." ### ";//prints the href
preg_match('#descuentos-(.*)/#', $href, $match);
echo $link->nodeValue." - ".$match[1]."<br/>";
}
输出(添加utf-8标头以查看正确的字符):
http://website.com/descuentos-espana/ ### Badajoz - espana
http://website.com/descuentos-espana/ ### Badalona - espana
http://website.com/descuentos-barcelona/ ### Barcelona - barcelona
http://website.com/descuentos-bilbao/ ### Bilbao - bilbao
http://website.com/descuentos-espana/ ### Burgos - espana
http://website.com/descuentos-espana/ ### Cáceres - espana
http://website.com/descuentos-cadiz/ ### Cádiz - cadiz
http://website.com/descuentos-espana/ ### Cartagena - espana
http://website.com/descuentos-espana/ ### Castellón - espana
http://website.com/descuentos-espana/ ### Ceuta - espana
http://website.com/descuentos-espana/ ### Ciudad Real - espana
http://website.com/descuentos-cordoba/ ### Córdoba - cordoba
http://website.com/descuentos-espana/ ### Cuenca - espana
Time elapsed: 0.000319004058838