I would like to add the site root path for those anchor tags that do not have a root path using the php dom document, so far they have made a function to do this using the str_replace function, but for some links three and in time root paths have been added. Then what should I edit in this function.
Problem : = The problem is that it adds three for the root time path for each anchor tag, and not for some. The $ HTML variable has many anchor tags, about 200 links. And also for images.
I know this is a very dirty question, but what I missed, I canβt get.
function addRootPathToAnchor($HTML) { $tmpHtml = ''; $xml = new DOMDocument(); $xml->validateOnParse = true; $xml->loadHTML($HTML); foreach ($xml->getElementsByTagName('a') as $a ) { $href = $a->getAttribute('href'); if(strpos($href,'www' > 0)) continue; else $HTML = str_replace($href,"http://www.mysite.com/".$href,$HTML); } return $HTML; }
source share