<?php $value['pagetext']='<a href="showthread.php?threadid=3830016&forumid=1852">http://xxxx.com/showthread.php?threadid=3830016&forumid=1852</a>'; $pattern="/showthread\.php\?threadid=(.+)&forumid=.[^\"]+?/"; preg_match_all($pattern,$value['pagetext'],$out); foreach($out[0] as $key=>$vo){ $value['pagetext']=str_replace($vo,$out[1][$key],$value['pagetext']); }echo $value['pagetext']."<hr>"; $pattern="/href=\"showthread\.php\?threadid=(.[^\"]+)\"/"; preg_match_all($pattern,$value['pagetext'],$matches); foreach($matches[0] as $key=>$vo){ $value['pagetext']=str_replace($vo,'href="'.$matches[1][$key].'"',$value['pagetext']); } echo "<pre>"; print_r($out);die; /* by wkj 2016-01-14 把$pattern="/showthread\.php\?threadid=(.+)&forumid=.[^\"]+?/"; 修改爲這樣的$pattern="/showthread\.php\?threadid=(\d+)&forumid=\d+?/"; 沒修改的時候致使 像上面的的標籤匹配一直出錯。。好久以前寫的正則仍是 too native 買了一本精通正則表達式,不得不說這本書很不錯,慢慢學習 */