原本链接:<a href="http://www.csdn.net/1.html">链接1</a><a href="/post-1.html">链接1</a>
<a href="/post-2.html">链接2</a>
<a href="/post-3.html">链接3</a>
<a href="/post-4.html">链接4</a>
 
如何替换为:<a href="http://www.csdn.net/1.html">链接1</a> (该条链接不变)
 
<a href="/m/?post-1">链接1</a>
<a href="/m/?post-2">链接2</a>
<a href="/m/?post-3">链接3</a>
<a href="/m/?post-4">链接4</a>
 

解决方案 »

  1.   

    $s =<<< TXT
    <a href="http://www.csdn.net/1.html">链接1</a><a href="/post-1.html">链接1</a>
    <a href="/post-2.html">链接2</a>
    <a href="/post-3.html">链接3</a>
    <a href="/post-4.html">链接4</a>
    TXT;
    $s = preg_replace('#href="/#', '$0m/?', $s);
    echo $s;<a href="http://www.csdn.net/1.html">链接1</a><a href="/m/?post-1.html">链接1</a>
    <a href="/m/?post-2.html">链接2</a>
    <a href="/m/?post-3.html">链接3</a>
    <a href="/m/?post-4.html">链接4</a>
      

  2.   


    替换出来的网址只m/?post-4.html 而不是 m/?post-4 html都去除不了……
      

  3.   

    $s=<<<html
    <a href="http://www.csdn.net/1.html">链接1</a>
     
    <a href="/post-1.html">链接1</a>
     <a href="/post-2.html">链接2</a>
     <a href="/post-3.html">链接3</a>
     <a href="/post-4.html">链接4</a>
    html;
    echo preg_replace('#(<a href="/)([^.]+)\.html(.+)#m','$1m/?$2$3',$s);