PHP: replacing absolute urls in a textarea

2019-08-10 08:18发布

Thanks to @Mihai Stancu I got a function that replaces relative urls to absolute urls. I improved it so it is doing it for href and src values.

I have got one domain with one calendar on and I am transferring some of the events onto another domain where I use these events too. I own both domains so there is no security risk in creating absolute urls.

But the function has a bug - also it replaces absolute links so http://www.example.com/... becomes http://www.example.net/http://www.example.com/... Can you help?

Please feel free to improve the function if you like :-)

<?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;
?>

1条回答
Melony?
2楼-- · 2019-08-10 08:35

Eyes still bleeding from the regexp.

How about 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();

Disclaimer: this returns a whole HTML document instead of a fragment; if you want a fragment, you can call saveHTML on the body tag instead.

查看更多
登录 后发表回答