<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> <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> <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()函数弄了一晚上了,也没弄出来,不知道各位高手有没有好的解决办法?
<td align='left' style='border-bottom:1px dotted #ccc;height:24px;padding-left:6px'><IMG SRC='images/qie_34.jpg' WIDTH=10 HEIGHT=10> <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> <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()函数弄了一晚上了,也没弄出来,不知道各位高手有没有好的解决办法?
解决方案 »
- 能不能按不同的按钮提交到不同的页面
- PHP生成验证码的时候不能生成图片,都是缺省图片的图片标 有图有代码 GD库使用正常
- 请人看看这里哪里有错误
- 刚学PHP .$str.的点事什么意思???迷糊中!
- php和xml的问题,在线等
- 显示mysql_fetch_array()函数有错是怎么回事?
- 菜鸟问题,SQL中的变量一定要用'".$abc."' 这样的方式连接吗,直接写$abc也行吧
- 服务器上和本地测试不一样????
- php程序员请进
- 我的redhat 7.3 +apache 2.0.47+php4.3.3里php.ini有问题!
- 类之间调用,哪种好呢?新手老手答案都有帮助!
- 又是关于汉字编码问题
先假设你的页面就只有这一行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');
});
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> <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);
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> <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方式,因为逻辑比较清晰