I have written a small piece of code that loads HTML content. Processes each image and checks if it has a full URL set.
If not, it adds it.
Currently I am doing this with XPath. But I get the feeling that the procedure gets quite CPU intensive. Hence I would like to know if there would be a faster method for what I am doing currently.
The code I have written
///######## START A NEW DOM DOCUMENT
$dom = new DOMDocument;
///######## LOAD THE HTML INTO THE DOM DOCUMENT
$dom->loadHTML($html);
///######## GET ALL IMAGES WITH XPath
$Images = $dom->getElementsByTagName('img');
///######## SETUP AN EMPTY SOURCE AND TARGET ARRAY
$SourceArray = $TargetArray = array();
///######## RUN THROUGH ALL SET IMAGE NODES
foreach ($Images as $node) {
///######## GET THE URL ATTRIBUTE
$SetURL = $node->getAttribute('src').PHP_EOL;
///######## IF THE URL DOES NOT START WITH HTTP
if(substr($SetURL, 0, 4) != 'http'){
///######## SET THE URL TO THE SOURCE ARRAY
$SourceArray[] = 'img src="'.$SetURL;
///######## SET THE TARGET URL
$TargetArray[] = 'img src="'.__APPBASE__.'/'.$this->TEMPLATEDIR.'/'.$SetURL;
}
}
///######## REMOVE THE BREAKS FROM BOTH ARRAYS
$SourceArray = str_replace(PHP_EOL, NULL, $SourceArray);
$TargetArray = str_replace(PHP_EOL, NULL, $TargetArray);
///######## REPLACE THE HTML TEXT
$html = str_replace($SourceArray, $TargetArray, $html);
///######## UNSET THE VARIABLES {free up memory}
unset($dom);
unset($SourceArray);
unset($TargetArray);