Created
December 4, 2016 02:05
Star
You must be signed in to star a gist
[ Posted by Imran Hamzah ] Change image src php code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$question_data = '<p>My questions here....</p><p> </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