I have a dynamic php code as follows
$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
and $mycontents
are changing depending on the data fetched, even links contained changes. Any function that will SCAN for the variable $mycontents
and if found any link with a or src, check first to see if is absolute, if yes, leave it, if no, using str_replace str_replace("src=\"", "src=\"".$url, $mycontents)
and change all links before echoing $mycontents, Target is, once $mycontents get echoed, has to be modified with all relative links scanned and found changed to absolute.
First you should identify exactly what attributes you need to be looking for and checking their values. The simplest check of course is to simply look for URLcontaining http(s)://
at the beginning of them and classing them as absolute already, anything else needs to be replaced.
These attributes would be href
and src
, as a base.
Of course, it's a very rudimentary check but it should suit your needs at least for now, barring any edge cases that may arise.