I will show you a php example instead of an image src om html string. I'll show you about how to replace src image with powerful HTML thread in php. i like to share with you php replace img tag with thread simply explain step by step restore image src to html string php.

Sometimes we need to replace src with a strong html thread with php. i mean that if you save the html thread in the database and you have to update the src path of that string when it shows on the front side. so here i will give you a simple example of converting src image to html thread using php.

Let's see below index.php file and output to easily understand how it works using DOMDocument.

index.php

<?php
  
$htmlString = '<strong>Image One:</strong><br/>
<img src="imageone.png" /><br/>
  
<strong>Image Two:</strong><br/>
<img src="imagetwo.png" /><br/>
  
<strong>Image Three:</strong><br/>
<img src="imagethree.png" /><br/>';
  
$doc = new DOMDocument();
$doc->loadHTML($htmlString);
$tags = $doc->getElementsByTagName('img');
   
foreach ($tags as $tag) {
   $oldSrc = $tag->getAttribute('src');
   $newScrURL = 'upload/images/'.$oldSrc;
   $tag->setAttribute('src', $newScrURL);
   $tag->setAttribute('data-src', $oldSrc);
} 
  
$htmlString = $doc->saveHTML();
  
print($htmlString);
?>