首頁 >後端開發 >php教程 >新手简单的正则表达式,大大们帮帮忙.多谢

新手简单的正则表达式,大大们帮帮忙.多谢

WBOY
WBOY原創
2016-06-13 12:09:36988瀏覽

新手求一个简单的正则表达式,大大们帮帮忙.谢谢

本帖最后由 bupabupa166 于 2014-10-31 22:08:43 编辑 women bags backpack wallet bag men keysool bag dress handbag shoulder bag purse shoes bag man leather bag


请问怎么用正则匹配到所有“data-text”的属性值(引号里面的值)。
谢谢
------解决思路----------------------
<?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 />}

------解决思路----------------------
写了一个比较直观的,先提权单链接,然后在单链接中提取data-text="...."引号中的信息
<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 />


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn