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;
?>
Eyes still bleeding from the regexp.
How about DOMDocument? :)
Disclaimer: this returns a whole HTML document instead of a fragment; if you want a fragment, you can call
saveHTML
on thebody
tag instead.