PHP Tutorial
Home
PHP Install Xampp
PHP Syntax & Comments
PHP Variables
PHP Constants
PHP Data Types
PHP Echo & Print
PHP Strings
PHP If...Else...Elseif
PHP Ternary Operator
PHP Loops
PHP Functions
PHP Arrays
PHP GET & POST
PHP Advanced
PHP Date and Time
PHP Include and Require
PHP File Upload
PHP Sessions
PHP Cookies
PHP Send Email
PHP JSON Parsing
PHP MySQL Database
PHP MySQL Introduction
PHP Connect to MySQL
PHP MySQL Create DB
PHP MySQL Create Table
PHP MySQL Insert Data
PHP MySQL Select Data
PHP MySQL Delete Data
PHP MySQL Update Data
PHP MySQL Where
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); ?>