日期:2014-05-17  浏览次数:20491 次

正则表达式问题
正则怎样匹配href="xxxabc/efghijk.css"类似这样的字符串中,href="后面的地址不以http开头的字串

比如有两个字符串:
<link href="images/css.css" rel="stylesheet" type="text/css">
<a href="http://www.abcdefg.com/news_show.asp?id=294">

我只想匹配第一个(即href的值不以http开头的), 并把它替换成<link href=""http://www.abcdefg.com/images/css.css" rel="stylesheet" type="text/css">, 即使相对的链接变成绝对链接地址.
$str = '<link href="images/css.css" rel="stylesheet" type="text/css">';
$str = preg_replace("/href=\"((?<!http)[^\"]*?)/i","http://www.btdair.com/\1",$str);
为什么不行?


------解决方案--------------------
额,我尝试弄了一下,弄不出来,不过建议你一步一步来,比如说用这样的办法


<link href="images/css.css" rel="stylesheet" type="text/css">
<a href="http://www.abcdefg.com/news_show.asp?id=294">

保存在in文件里,用grep慢慢调试

#grep --color -P "href=\"[^h][^\"]*\"" in
<link href="images/css.css" rel="stylesheet" type="text/css">