Create a gist now

Instantly share code, notes, and snippets.

[ Posted by Imran Hamzah ] Change image src php code.
<?php
$question_data = '<p>My questions here....</p><p>&nbsp;</p><p><img class="img-responsive" src="/uploads/images/questions/93_20161017102613.jpg" style="max-height:400px;" /></p>';
$xml = new DOMDocument();
$xml->loadHTML($question_data);
//Initial Dom
//Find only image and convert url
//Process to convert url
$imgNodes = $xml->getElementsByTagName('img');
$arr_image_file_names = [];
$new_src = '';
for ($i = $imgNodes->length - 1; $i >= 0; $i--) {
$imgNode = $imgNodes->item($i);
$image_file_names = pathinfo($imgNode->getAttribute('src'), PATHINFO_BASENAME);
if(!empty($image_file_names)):
// Replace with new src
$new_src = 'http://myurl/qst/def/img/v1/'.$image_file_names;
endif;
}
echo ($new_src);
//Update into new array
//Convert back to DOM
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment