有些時候,我們需要對一些html文本進行處理,比如需要將文本中的超鏈接內容去除,這個時候就需要用到正則表達式了。比如你可以用$str = preg_replace("/<a[^>]*href=[^>]*>|<\/[^a]*a[^>]*>/i","",$strhtml); 這段來實現需求,如果想要更多解決方法,可以參看以下的。
1、刪除內容中的超鏈接
ereg_replace('<a([^>]*)>([^<]*)</a>','<font color="red">\\2</font>',$content);
ereg_replace("<a [^>]*>|<\/a>","",$content);
2、消除包含特定詞的超鏈接
$find="this string is my find";
$string='<font color="red">替換掉了</font>';//將超鏈接替換成的內容
echo ereg_replace('<a([^>]*)>([^<]*'.$find.'[^>]*)</a>','<font color="red">\\2</font>',$content);
3、獲取超鏈接文本內容
//方法一
preg_match_all('/<(a|a)[s]{0,1}[w=":()]*>[nrn]*(check user)[nrn]*</(a|a)>/i',$string,$matches);
//方法二
preg_match_all('/<a[dd]*>check user</a>/i',$string,$matches);
print_r($matches);
//方法三
preg_match_all('/<a[^>]*>[^<]*</a>/i',$string,$matches);
print_r($matches);
//方法四
preg_match_all('/<a.+?>check user</a>/is',$str,$arr);
print_r($arr);
//方法五
preg_match_all('/<a.+?>check user</a>/is',$str,$arr);
print_r($arr);