比如
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"
如果是本身就是绝对地址,就不替换.能解决的话,再给一百分,多谢了
顶者有分...
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"
如果是本身就是绝对地址,就不替换.能解决的话,再给一百分,多谢了
顶者有分...
dirname(__FILE__)獲取當前文件的絕對路徑$URL = $_SERVER['HTTP_HOST'].dirname(__FILE__)
那么我想把它们变成绝对路径,以便能正常显示
<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);
$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;
$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;
查找:{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"
你可以这样:<!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>
还有,如果写域名,记得一定要把“http://”写上,否则浏览器会把域名当做文件夹处理的。
这样还管用吗