How to Replace Image SRC in a Dynamic HTML String with PHP

How to Replace Image SRC in a Dynamic HTML String with PHP

Learn how to replace image src in a dynamic HTML string with PHP. I would like to share with you PHP replace img tag in string. I explained simply step by step replace image src in HTML string PHP.

I am going to show you example of php replace image src om html string. I’m going to show you about How to replace image src in a dynamic HTML string in php. i would like to share with you php replace img tag in string. i explained simply step by step replace image src in html string php.

Sometime we need to change src in a dynamic html string with php. i mean if you store html string into database and you have to update src path for that string when you display in front end side. so here i will give you very simple example of replace image src in html string using php.

Let' see bellow index.php file and output so you can 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);

php image web-development programming developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

PHP Web Application Development Company

As a top **PHP Web Application Development Company in USA**[https://www.dataeximit.com/php-development-services/](https://www.dataeximit.com/php-development-services/ "https://www.dataeximit.com/php-development-services/"), we at Data EximIT have...

PHP Website Development

Skenix Infotech is a leading PHP Web Development Company that helps companies get results with industry best practices. Get affordable PHP Development Services.

Hire Dedicated PHP Developer

Looking to hire affordable yet experienced PHP developers? **[Hire Dedicated PHP Developer](https://hourlydeveloper.io/hire-dedicated-php-developer/ "Hire Dedicated PHP Developer")**, who can convert your idea to reality, within the stipulated...

Hire PHP Developer

Looking to develop a PHP based website from scratch or revamp your existing website? **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")** has always been an industry leader for companies and business owners looking to hire...

Advantages of Hiring PHP Developer for your Website Project

PHP - Hypertext pre-processor, a scripting language used by many people in developing web pages, but most of us are unaware even of the full form. To train someone and make them learn this whole language is as difficult and time-consuming as it is...