Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.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替换html页面正文中的所有链接_Php_Html - Fatal编程技术网

使用PHP替换html页面正文中的所有链接

使用PHP替换html页面正文中的所有链接,php,html,Php,Html,我使用以下代码替换HTML页面上的所有链接 $output = file_get_contents($turl); $newOutput = str_replace('href="http', 'target="_parent" href="hhttp://localhost/e/site.php?turl=http', $output); $newOutput = str_replace('href="www.', 'target="_parent" href="http://localh

我使用以下代码替换HTML页面上的所有链接

$output = file_get_contents($turl);
$newOutput = str_replace('href="http', 'target="_parent"  href="hhttp://localhost/e/site.php?turl=http', $output);
$newOutput = str_replace('href="www.', 'target="_parent"  href="http://localhost/e/site.php?turl=www.', $newOutput);
$newOutput = str_replace('href="/', 'target="_parent"  href="http://localhost/e/site.php?turl='.$turl.'/', $newOutput);

echo $newOutput;

我想修改此代码,只替换身体内部的链接,而不替换头部的链接。

您可以将代码斩首。
查找主体并将头部与主体分离为两个变量

//$output = file_get_contents($turl);

$output = "<head> blablabla

 Bla bla
</head>
<body>
 Foobar
 </body>";

//Decapitation
$head = substr($output, 0, strpos($output, "<body>"));
$body = substr($output, strpos($output, "<body>"));
// Find body tag and parse body and head to each variable

$newOutput = str_replace('href="http', 'target="_parent"  href="hhttp://localhost/e/site.php?turl=http', $body);
$newOutput = str_replace('href="www.', 'target="_parent"  href="http://localhost/e/site.php?turl=www.', $newOutput);
$newOutput =  str_replace('href="/', 'target="_parent"  href="http://localhost/e/site.php?turl='.$turl.'/', $newOutput);

echo $head . $newOutput;
/$output=文件获取内容($turl);
$output=“blabla”
布拉布拉
福巴
";
//斩首
$head=substr($output,0,STRPO($output,”);
$body=substr($output,STRPO($output,”);
//查找body标记并将body和head解析为每个变量
$newOutput=str\u replace('href=“http”,“target=“\u parent”href=”hhttp://localhost/e/site.php?turl=http“,$body);
$newOutput=str_replace('href=“www.”,'target=“\u parent”href=”http://localhost/e/site.php?turl=www.“,$newOutput);
$newOutput=str_replace('href=“/”,'target=“\u parent”href=”http://localhost/e/site.php?turl=“.$turl./”,$newOutput);
回声头$新产量;

您可以使用来解析和操作源代码。对于这样的任务,最好使用专用解析器,而不是使用字符串操作

// Parse the HTML into a document
$dom = new \DOMDocument();
$dom->loadXML($html);

// Loop over all links within the `<body>` element
foreach($dom->getElementsByTagName('body')[0]->getElementsByTagName('a') as $link) {
    // Save the existing link
    $oldLink = $link->getAttribute('href');

    // Set the new target attribute
    $link->setAttribute('target', "_parent");

    // Prefix the link with the new URL
    $link->setAttribute('href', "http://localhost/e/site.php?turl=" . urlencode($oldLink));
}

// Output the result
echo $dom->saveHtml();
//将HTML解析为文档
$dom=new\DOMDocument();
$dom->loadXML($html);
//在``元素内的所有链接上循环
foreach($dom->getElementsByTagName('body')[0]->getElementsByTagName('a')作为$link){
//保存现有链接
$oldLink=$link->getAttribute('href');
//设置新的目标属性
$link->setAttribute('target','u parent');
//在链接前面加上新的URL
$link->setAttribute('href',”http://localhost/e/site.php?turl=“.urlencode($oldLink));
}
//输出结果
echo$dom->saveHtml();

我看到了你的评论,你需要保持冷静。看看我的最新答案如果我还想向url添加参数呢<代码>$link->setAttribute('href',”http://localhost/e/site.php?turl=“.urlencode($oldLink)。”&somethingElse=1“将导致href为:
http://localhost/e/site.php?turl=oldurl&somethingElse=1
。我怎样才能告诉
DOMDocument
不显式地编码符号?基本上,我想做的是在href中获取一个url,该url可能有许多参数,然后通过另一个url发送它,在另一个参数中添加一个哈希。