plus/list.php?tid=1替换成plus/list-1.htmltid=后面那个是正整数,替换为list-后面的数字

解决方案 »

  1.   

    <?php 
    $str = "plus/list.php?tid=1";
    $pattern = "/\.php\?tid=(\d+)/";
    $replacement = "-$1.html";
    print preg_replace($pattern, $replacement, $str);
    ?>
    是否像这样?
      

  2.   


    tid=1 这个1不是固定的,是数字来的.
      

  3.   


    $url ="plus/list.php?tid=1";
    echo preg_replace("#plus/list.php\?tid=(.*)#", "plus/list-\${1}.html", $url);
      

  4.   


    你用(.*)#是匹配数字吗?因为要被替换的代码全部是这样的:
    <A href="/plus/list.php?tid=1">服饰</A> 
    &gt; <A href="/plus/list.php?tid=11">服饰搭配</A> &gt; <A 
    href="/plus/list.php?tid=17">场合搭配</A> &gt; <A 
    href="/plus/list.php?tid=20">职场</A> &gt;
      

  5.   

    .是匹配除换行符以外的任意字符的 *是任意个
    这个地方你要是匹配数字的话用 \d 
    +表示1个或1个以上
    所以你可以改为$url ="plus/list.php?tid=1";
    echo preg_replace("#plus/list.php\?tid=(\d+)#", "plus/list-\${1}.html", $url);
      

  6.   

    [0-9]\{1,\}
    正则匹配数字 出现一次以上 在linux下是这样 
    根据情况\有可能不需要
      

  7.   

    $str = '
    <A href="/plus/list.php?tid=1">服饰</A> 
    &gt; <A href="/plus/list.php?tid=11">服饰搭配</A> &gt; <A 
    href="/plus/list.php?tid=17">场合搭配</A> &gt; <A 
    href="/plus/list.php?tid=20">职场</A> &gt;
    ';$s = preg_replace("/list.php\?tid=(\d+)/i", "list-\\1.html", $str);
    echo $s;嘛,再根據你的頁面鏈接的要求繼續正它
    如:加上plus/ 等等, 只替換 有帶plus/的鏈接