Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检查相对URL并替换为绝对URL_Php_Dom - Fatal编程技术网

Php 检查相对URL并替换为绝对URL

Php 检查相对URL并替换为绝对URL,php,dom,Php,Dom,我有一个动态php代码,如下所示 $url = "http://www.myweb.com/home"; $mycontents = 'These are the contents with <a href="myweb/local.php" />relative link</a> and <a href="http://www.myweb.com/home/loc.php"> absolute link</a> and some images w

我有一个动态php代码,如下所示

$url = "http://www.myweb.com/home";
$mycontents = 'These are the contents with <a href="myweb/local.php" />relative link</a> and <a href="http://www.myweb.com/home/loc.php"> absolute link</a> and some images with <img src="myweb/local.jpg" /> with relative link and <img src="http://www.myurl.com/pictures/pic.jpg" />';
$url=”http://www.myweb.com/home";
$mycontents='这些是带有和的内容,一些图像带有相对链接和';

$url
$mycontents
根据获取的数据而变化,即使链接包含变化。任何将扫描变量
$mycontents
的函数,如果发现任何带有a或src的链接,首先检查是否是绝对的,如果是,则保留它,如果不是,则使用stru replace
str\u replace(“src=\”,“src=\”.$url,$mycontents)
并在回显$mycontents之前更改所有链接,一旦回显$mycontents,目标是,必须在扫描所有相对链接并将其更改为绝对链接的情况下进行修改。

首先,您应该准确确定需要查找哪些属性并检查其值。当然,最简单的检查方法是在URL开头查找包含http(s)://的URL,并将其归类为绝对值,其他任何内容都需要替换

这些属性将是
href
src
,作为基础


当然,这是一个非常基本的检查,但至少现在应该适合您的需要,除非出现任何边缘情况。

我正在从几个网站获取数据,这些网站的内容可能包含相对或绝对链接,网站url如上面显示的$url所示,但在回显$mycontents之前,必须检查链接和图像的相对或绝对Url,如果是相对的,则更改为绝对的,如果是绝对的,则保持原样。。请帮助我,我现在被卡住了…任何PHP函数都可以检查并更改,请…我的头很痛!您可以使用正则表达式来定位和标识需要替换的字符串。深入研究,我认为这比您真正需要的更复杂,但它从一些测试字符串中得出了我所期望的结果:
(href | src)=(((?!http\:)[a-z0-9\/\\\.])*)\2\I