原本链接:<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="/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=<<<html
    <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 str_replace('.html','',preg_replace('#(?<=")/#','/m/?',$s));
      

  2.   

    补充原本链接:
     
    <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>  
      

  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);