Skip to content

Instantly share code, notes, and snippets.

@phpfiddle
Created December 4, 2016 02:05
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save phpfiddle/f03670540f732f3bb1297bb388f146bb to your computer and use it in GitHub Desktop.
[ 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