<tr>
        <td align='left' style='border-bottom:1px dotted #ccc;height:24px;padding-left:6px'><IMG SRC='images/qie_34.jpg' WIDTH=10 HEIGHT=10>&nbsp;<a href='content.php?url=/2011-04/03/cms446666article.shtml' target='_blank' class='title'>铲子岩隧道正式开工</a></td>
        <td align='left' width='80px' style='border-bottom:1px dotted #ccc;height:24px'><font class=title>(2011-4-3)</font></td>
    </tr>如上述所示的一段html代码(或者说是一段字符串),我需要利用一个正则表达式,将其中的文字部分(铲子岩隧道正式开工)和日期(2011-4-3)作为参数放到url中去,最后的效果如下:
<tr>
        <td align='left' style='border-bottom:1px dotted #ccc;height:24px;padding-left:6px'><IMG SRC='images/qie_34.jpg' WIDTH=10 HEIGHT=10>&nbsp;<a href='content.php?url=/2011-04/03/cms446666article.shtml&title=铲子岩隧道正式开工&date=2011-4-3' target='_blank' class='title'>铲子岩隧道正式开工</a></td>
        <td align='left' width='80px' style='border-bottom:1px dotted #ccc;height:24px'><font class=title>(2011-4-3)</font></td>
    </tr>自己用php自带的eregi_replace()函数弄了一晚上了,也没弄出来,不知道各位高手有没有好的解决办法?

解决方案 »

  1.   

    把html都remove掉,不就完了吗?
      

  2.   

    这个用正则不太合适吧。用前端JQ来操作简单快速!
    先假设你的页面就只有这一行TR$(function(){
       $("td").eq(0).find("a").attr('href','content.php?url=/2011-04/03/cms446666article.shtml&title=$("td").eq(0).find("a").val()&date=2011-4-3');
     });
      

  3.   

    这种问题还是交给dom处理的好!
      

  4.   


    header("Content-type:text/html;charset=gbk");
    $s = <<<eof
    <tr>
            <td align='left' style='border-bottom:1px dotted #ccc;height:24px;padding-left:6px'><IMG SRC='images/qie_34.jpg' WIDTH=10 HEIGHT=10>&nbsp;<a href='content.php?url=/2011-04/03/cms446666article.shtml' target='_blank' class='title'>铲子岩隧道正式开工</a></td>
            <td align='left' width='80px' style='border-bottom:1px dotted #ccc;height:24px'><font class=title>(2011-4-3)</font></td>
        </tr>
    eof;
    $f = "/<a\s*href=\'(.*?)\'[^>]*>(.*?)<\/a>[^\(]*\((.*?)\)<\/font>/is";
    $t = "<a href='$1&title=$2&date=$3' target='_blank' class='title'>$2</a></td>
            <td align='left' width='80px' style='border-bottom:1px dotted #ccc;height:24px'><font class=title>($3)</font>";
    echo $out = preg_replace($f,$t,$s);
      

  5.   

    <?php 
    header("Content-type:text/html;charset=gbk");
    $s = <<<eof
    <tr>
            <td align='left' style='border-bottom:1px dotted #ccc;height:24px;padding-left:6px'><IMG SRC='images/qie_34.jpg' WIDTH=10 HEIGHT=10>&nbsp;<a href='content.php?url=/2011-04/03/cms446666article.shtml' target='_blank' class='title'>铲子岩隧道正式开工</a></td>
            <td align='left' width='80px' style='border-bottom:1px dotted #ccc;height:24px'><font class=title>(2011-4-3)</font></td>
        </tr>
    eof;
    $f = "/(<a\s*href=\'(.*?)\'[^>]*>(.*?)<\/a>[^\(]*\((.*?)\)<\/font>)/ise";
    $t = "<a href='$1&title=$2&date=$3' target='_blank' class='title'>$2</a></td>
            <td align='left' width='80px' style='border-bottom:1px dotted #ccc;height:24px'><font class=title>($3)</font>";
    $out = preg_replace($f,"parseURL('$1', '$2', '$3', '$4')",$s);
    echo $out;function parseURL($content, $url, $title, $data) {
    $newurl = $url . (strpos($url,'?') === false ? '?' : '&') . "title=$title&data=$data";

    return str_replace($url, $newurl, $content);
    }虽然可以实现,但是还是推荐使用dom方式,因为逻辑比较清晰