PHP:替换文本区域中的绝对网址


PHP: replacing absolute urls in a textarea

感谢@Mihai Stancu,我得到了一个将相对网址替换为绝对网址的函数。我改进了它,以便它针对 href 和 src 值执行此操作。

有一个带有一个日历的域,我正在将一些事件转移到另一个域,我也在其中使用这些事件。我拥有这两个域,因此创建绝对 URL 没有安全风险。

但是该功能有一个错误 - 它也替换了绝对链接,所以 http://www.example.com/......变得 http://www.example.net/http://www.example.com/...你能帮忙吗?

如果您愿意,请随时改进功能:-(

<?php 
$domain = 'http://www.example.net/'; // notice the domain has an end slash
$textarea = 'tester afadf adf <a href="http://www.example.com/folder1/page1.html">do not replace this</a> ... bla bla <a href="/folder2/page2.html">do replace this url</a> bla bla.... <img src="http://www.example.com/somefolder/somepic.jpg" /> <img src="/somefolder/somepic.jpg" />';
$tags = array("href", "src");
foreach ($tags as $tag) { 
    $textarea = preg_replace('/'.$tag.''s*='s*(?<'.$tag.'>"[^''"]*"|''[^'''']*'')/e', 'expand_links($tag, $domain, "$1")', $textarea);
}
function expand_links($tag, $domain, $link) {
    return($tag.'="'.$domain.trim($link, '''"/''').'"');
}
echo $textarea;
?>

眼睛仍然从正则表达式中流血。

DOMDocument怎么样?:)

$domain = 'http://www.example.net/'; // notice the domain has an end slash
$textarea = 'tester afadf adf <a href="http://www.example.com/folder1/page1.html">do not replace this</a> ... bla bla <a href="/folder2/page2.html">do replace this url</a> bla bla.... <img src="http://www.example.com/somefolder/somepic.jpg" /> <img src="/somefolder/somepic.jpg" />';
// wrap fragment into a full HTML body first (making sure the content type is set properly)
$full_doc = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>' . $textarea . '</body></html>';
$d = new DOMDocument;
libxml_use_internal_errors(true); // muffle any errors from libxml
$d->loadHTML($textarea);
libxml_clear_errors(); // clear the errors found
$x = new DOMXPath($d);
// find all tags with either href or src attribute
foreach ($x->query('//*[@href|@src]') as $e) {
    $attr = $e->getAttributeNode('href') ?: $e->getAttributeNode('src');
    if (!preg_match('#^(?:https?://|mailto:)#', $attr->nodeValue)) {
        // not absolute
        $attr->nodeValue = $domain . $attr->nodeValue;
    }
}
echo $d->saveHTML();

免责声明:这将返回整个 HTML 文档而不是片段;如果你想要一个片段,你可以改为在 body 标记上调用saveHTML