求用正则表达式把html的字符串里的链接,图片都变为绝对路径?

110来电 2009-08-13 08:47:07
比如
src="images/screenshots/3.jpg"
href="styles/sifrscreen.css"
<script src="scripts/functions.js"

如果是"images/" 替换为 "www.xxx.com/site1/images"
如果是"/images/" 替换为 "www.xxx.com/images"
如果是本身就是绝对地址,就不替换.

能解决的话,再给一百分,多谢了
顶者有分...
...全文
227 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 jnkc 的回复:]
关键在于:html的head去加base用抓取网址,页面中所有自己的链接、资源一律写全网址。
还有,如果写域名,记得一定要把“http://”写上,否则浏览器会把域名当做文件夹处理的。
[/Quote]
那请问如果他抓取的网页网址是 http://www.sss.com/a.php
这样还管用吗
江南昆虫 2009-08-14
  • 打赏
  • 举报
回复
关键在于:html的head去加base用抓取网址,页面中所有自己的链接、资源一律写全网址。
还有,如果写域名,记得一定要把“http://”写上,否则浏览器会把域名当做文件夹处理的。
江南昆虫 2009-08-14
  • 打赏
  • 举报
回复
从你的目的性来看,你也可以换一种角度来做,根本不需要去替换。
你可以这样:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>这里是标题</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://www.我自己的网址.com/myscript.js"></script>
<base href="http://www.抓取的网址.com/site1/" />
</head>
<body>
<img src="http://www.我自己的网址.com/images/abc.jpg" />
<!-- 抓取的内容 开始 -->
<img src="images/screenshots/3.jpg" />
<link rel="stylesheet" type="text/css" href="styles/sifrscreen.css" />
<script src="scripts/functions.js"></script>
<img src="/images/screenshots/3.jpg" />
<link rel="stylesheet" type="text/css" href="/styles/sifrscreen.css" />
<script src="/scripts/functions.js"></script>
<!-- 抓取的内容 结束 -->
</body>
</html>

纠结的程序猿 2009-08-14
  • 打赏
  • 举报
回复
下载PilotEdit 2.7, http://topic.csdn.net/u/20090723/07/33b39399-81ad-4fb1-a4a2-78509d2161a3.html

用下面的正则表达式查找替换(做两次替换):
查找:{src|href}="{images|styles|scripts}
替换为:%01="www.xxx.com/%03
查找:{src|href}="/
替换为:%01="www.xxx.com/


假设原始文件为:
src="images/screenshots/3.jpg"
href="styles/sifrscreen.css"
<script src="scripts/functions.js"
src="/images/screenshots/3.jpg"
href="/styles/sifrscreen.css"
<script src="/scripts/functions.js"



将被转换为:
src="www.xxx.com/images/screenshots/3.jpg"
href="www.xxx.com/styles/sifrscreen.css"
<script src="www.xxx.com/scripts/functions.js"
src="www.xxx.com/images/screenshots/3.jpg"
src="www.xxx.com/styles/sifrscreen.css"
<script src="www.xxx.com/scripts/functions.js"
countstars 2009-08-14
  • 打赏
  • 举报
回复
测试这个看看,赫赫
$str = file_get_contents('http://www.mozillaonline.com/');
$str = preg_replace('/(?<=src=[\'"])(?!(\/|http))/sm', 'http://www.mozillaonline.com/', $str);
$str = preg_replace('/(?<=src=[\'"])(?=\\/)/sm', 'http://www.mozillaonline.com', $str);
$str = preg_replace('/(?<=href=[\'"])(?!(\/|http))/sm', 'http://www.mozillaonline.com/', $str);
$str = preg_replace('/(?<=href=[\'"])(?=\\/)/sm', 'http://www.mozillaonline.com', $str);
echo $str;
countstars 2009-08-14
  • 打赏
  • 举报
回复
多了个斜杠,小改了下
$str = '<img src="images/screenshots/3.jpg" />
<link href="styles/sifrscreen.css" />
<script src="scripts/functions.js" />
<img src="/images/aa.gif" />
';
$str = preg_replace('/(?<=src=[\'"])(?!(\/|http:))/sm', 'http://www.xxx.com/site1/', $str);
$str = preg_replace('/(?<=src=[\'"])(?=\\/)/sm', 'http://www.xxx.com', $str);
$str = preg_replace('/(?<=href=[\'"])(?!(\/|http:))/sm', 'http://www.xxx.com/site1/', $str);
$str = preg_replace('/(?<=href=[\'"])(?=\\/)/sm', 'http://www.xxx.com', $str);
echo $str;
countstars 2009-08-14
  • 打赏
  • 举报
回复
$str = ' 
<img src="images/screenshots/3.jpg" />
<link href="styles/sifrscreen.css" />
<script src="scripts/functions.js" />
<img src="/images/aa.gif" />
';
$str = preg_replace('/(?<=src=[\'"])(?!(\/|http:))/sm', 'http://www.xxx.com/site1/', $str);
$str = preg_replace('/(?<=src=[\'"])(?=\\/)/sm', 'http://www.xxx.com/', $str);
$str = preg_replace('/(?<=href=[\'"])(?!(\/|http:))/sm', 'http://www.xxx.com/site1/', $str);
$str = preg_replace('/(?<=href=[\'"])(?=\\/)/sm', 'http://www.xxx.com/', $str);
110来电 2009-08-13
  • 打赏
  • 举报
回复
[Quote=引用楼主 dingoishere 的回复:]
比如
src="images/screenshots/3.jpg"
href="styles/sifrscreen.css"
<script src="scripts/functions.js"

如果是"images/" 替换为 "www.xxx.com/site1/images"
如果是"/images/" 替换为 "www.xxx.com/images"
如果是本身就是绝对地址,就不替换.

[/Quote]
比如抓取的网站是www.abc.com,那么它的html里肯定有很多相对路径的图片和css拉
那么我想把它们变成绝对路径,以便能正常显示
CunningBoy 2009-08-13
  • 打赏
  • 举报
回复
你的要求描述不清,以下内容供參考:

$_SERVER['HTTP_HOST']取服務器的域名
dirname(__FILE__)獲取當前文件的絕對路徑

$URL = $_SERVER['HTTP_HOST'].dirname(__FILE__)
110来电 2009-08-13
  • 打赏
  • 举报
回复
我顶啊啊
前言:本资源来自于javaeye,原资源链接地址:http://www.javaeye.com/topic/67398 原文如下: 以前写了一个java的正规表达式的java工具类,分享一下,有用到的欢迎下载使用。 如果你有常用的定义好的,且测试通过的正规表达式,欢迎跟贴,也让我享用一下 . 类中用到了 jakarta-oro-2.0.jar 包,请大家自己在 apache网站下下载 在这是junit测试单元类我就不提交了,在main()方法中有几个小测试,有兴趣自己玩吧. 这个工具类目前主要有25种正规表达式(有些不常用,但那时才仔细深入的研究了一下正规,写上瘾了,就当时能想到的都写了): 1.匹配图象; 2 匹配email地址; 3 匹配匹配并提取url ; 4 匹配并提取http ; 5.匹配日期 6 匹配电话; 7 匹配身份证 8 匹配邮编代码 9. 不包括特殊字符的匹配 (字符串中不包括符号 数学次方号^ 单引号' 双引号" 分号; 逗号, 帽号: 数学减号- 右尖括号> 左尖括号 0) 12 匹配正整数 13 匹配非正整数(负整数 + 0) 14 匹配负整数; 15. 匹配整数 ; 16 匹配非负浮点数(正浮点数 + 0) 17. 匹配正浮点数 18 匹配非正浮点数(负浮点数 + 0) 19 匹配负浮点数; 20 .匹配浮点数; 21. 匹配由26个英文字母组成的字符串; 22. 匹配由26个英文字母的大写组成的字符串 23 匹配由26个英文字母的小写组成的字符串 24 匹配由数字和26个英文字母组成的字符串; 25 匹配由数字、26个英文字母或者下划线组成的字符串; java源码: /* * Created on 2005-4-15 * * Summary of regular-expression constructs 正则表达式结构简介: * Construct Matches * Characters 字符: * x The character x x 字符 x * \\ The backslash character \\ 反斜杠 * \0n The character with octal value 0n (0 <= n <= 7) \0n 十进制数 (0 <= n <= 7) * \0nn The character with octal value 0nn (0 <= n <= 7) \0nn 十进制数 0nn (0 <= n <= 7) * \0mnn The character with octal value 0mnn (0 <= m <= 3, 0 <= n <= 7) \0mnn 十进制数 0mnn (0 <= m <= 3, 0 <= n <= 7) * \xhh The character with hexadecimal value 0xhh \xhh 十六进制数 0x

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧