新手求一个简单的正则表达式,大大们帮帮忙.谢谢
<?php<br />$str='<a title="women bags" data-text="women bags" href="http://www.xxx.com/keys/i.html?_sacat=0&_nkw=women+bags&_frs=1"><b>women bags</b></a> <a title="backpack" data-text="backpack" href="http://www.xxx.com/keys/i.html?_sacat=0&_nkw=backpack&_frs=1"><b>backpack</b></a> <a title="wallet" data-text="wallet" href="http://www.xxx.com/keys/i.html?_sacat=0&_nkw=wallet&_frs=1"><b>wallet</b></a> <a title="bag men" data-text="bag men" href="http://www.xxx.com/keys/i.html?_sacat=0&_nkw=bag+men&_frs=1">bag <b>men</b></a> <a title="keysool bag" data-text="keysool bag" href="http://www.xxx.com/keys/i.html?_sacat=0&_nkw=keysool+bag&_frs=1"><b>keysool</b> bag</a> <a title="dress" data-text="dress" href="http://www.xxx.com/keys/i.html?_sacat=0&_nkw=dress&_frs=1"><b>dress</b></a> <a title="handbag" data-text="handbag" href="http://www.xxx.com/keys/i.html?_sacat=0&_nkw=handbag&_frs=1"><b>handbag</b></a> <a title="shoulder bag" data-text="shoulder bag" href="http://www.xxx.com/keys/i.html?_sacat=0&_nkw=shoulder+bag&_frs=1"><b>shoulder</b> bag</a> <a title="purse" data-text="purse" href="http://www.xxx.com/keys/i.html?_sacat=0&_nkw=purse&_frs=1"><b>purse</b></a> <a title="shoes" data-text="shoes" href="http://www.xxx.com/keys/i.html?_sacat=0&_nkw=shoes&_frs=1"><b>shoes</b></a> <a title="bag man" data-text="bag man" href="http://www.xxx.com/keys/i.html?_sacat=0&_nkw=bag+man&_frs=1">bag <b>man</b></a> <a title="leather bag" data-text="leather bag" href="http://www.xxx.com/keys/i.html?_sacat=0&_nkw=leather+bag&_frs=1"><b>leather</b> bag</a>';<br /><br />preg_match_all('/data-text="(.+)"/U',$str,$matches,PREG_PATTERN_ORDER );<br /><br />var_dump($matches[1]);
<br />array(12) {<br /> [0]=><br /> string(10) "women bags"<br /> [1]=><br /> string(8) "backpack"<br /> [2]=><br /> string(6) "wallet"<br /> [3]=><br /> string(7) "bag men"<br /> [4]=><br /> string(11) "keysool bag"<br /> [5]=><br /> string(5) "dress"<br /> [6]=><br /> string(7) "handbag"<br /> [7]=><br /> string(12) "shoulder bag"<br /> [8]=><br /> string(5) "purse"<br /> [9]=><br /> string(5) "shoes"<br /> [10]=><br /> string(7) "bag man"<br /> [11]=><br /> string(11) "leather bag"<br />}
<br />//先匹配超链接的前半部分,比如<a href="......" >这一部分<br />function filterLink($data)<br />{<br /> $pat='#<a[^>]+?>#ui';<br /> if(preg_match_all($pat, $data, $match))<br /> {<br /> foreach($match[0] as $link)<br /> {<br /> //echo $link."<br /><br />";<br /> filterData($link);<br /> }<br /> }<br /> else<br /> {<br /> echo '匹配失败';<br /> }<br />}<br /><br />//然后在单个的<a data-text="..." href="..."> 中提取需要的信息<br />function filterData($data)<br />{<br /> $pat='#data-text="([^"]+?)"#i';<br /> if(preg_match_all($pat, $data, $match))<br /> {<br /> foreach($match[1] as $content)<br /> {<br /> echo $content."<br />";<br /> }<br /> }<br /> else<br /> {<br /> echo '匹配失败';<br /> }<br />}<br />filterLink($str);<br />