嗨,我基本上是试图通过php来获取页面,获取它的html并将html(以突出显示一些关键字)更改为一点,并在我的页面(jquery)中将其显示为覆盖图。
//My php page data.php
<?php
$html= file_get_contents($_GET['url']);
echo $html;
?>
//My jquery ajax request to data.php from page main.html
function test()
{
$.ajax({
type: 'GET',
url: 'data.php',
data: 'url=http://www.developphp.com/view_lesson.php?v=338',
cache: false,
success: function(result)
{
$("#overlay").append(result);
}
});
}
}
如您所见,由于该网页使用相对URL,因此在重叠显示该网页时出现了问题。我尝试寻找一种将相对值转换为绝对值的方法,但没有发现任何有用的方法。你们能用正确的方式指出我吗?
我喜欢@charlietfl的解决方案。但是,我认为以某种方式在将已抓取的内容传递给客户端之前在服务器端进行操作更有意义。您可以使用DomDocument做到这一点。
以下代码在回显结果之前将所有 <img>
src
相对路径转换为绝对路径。对<a>
标签href
属性使用相同的方法,依此类推,
error_reporting(0); //suppress DOM errors
$basePath='http://www.developphp.com/'; //use parse_url to get the basepath dynamically
$content=file_get_contents('http://www.developphp.com/view_lesson.php?v=338');
$dom=new DomDocument();
$dom->loadHTML($content);
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
$src=$image->attributes->getNamedItem("src")->value;
if (strpos($basePath, $src)<=0) {
$image->attributes->getNamedItem("src")->value=$basePath.$src;
}
}
echo $dom->saveHTML();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句