Tutorial - PHP Replace Img Src From String
Tags: php, showdev
Published: 2018-04-03 05:15:56

---
title: Tutorial - PHP Replace Img Src From String
published: true
description: 
tags: php, showdev
canonical_url: https://ua1.us/news/tutorials/tutorial-php-replace-img-src-from-string/
---

<p style="text-align: left;">Recently, I had to figure out how to change &lt;img&gt; tag src using only PHP. I had to change the request from HTTP to HTTPS.  I was given the task to convert a site over to HTTPS. The website is managed on Big Commerce. As we already know, when you are changing a site over to HTTPS, you have to hunt down all of the images/assets of the site and convert them to request HTTPS as well. Otherwise, you will end up with the dreaded, mixed-content error and your site is deemed as unsecured by the browser you are visiting your site from.</p>
So it was easy enough to go through, page by page, and update images/assets to make requests over HTTPS instead of HTTP. Things were going along just fine until I ran into a big problem.
<h2>The Problem</h2>
For this particular client, products of the website are not managed inside of Big Commerce. Instead, they are managed in a third party ERP (Enterprise Resource Planning) software. Which syncs changes with the Big Commerce product pages. So what was found is that the product pages are essentially managed in the ERP. Any products that included images inside of the Product Descriptions included it as HTTP. So here we are...potentially thousands of products, all of which I would be going in an manually editing each product description containing <code><img />&lt;img src="http://www..."&gt;</code> to <code>&lt;img src="https://www..."&gt;</code>. Adding only an <strong>s </strong>to <strong>http://</strong>.
<h2>Solving The Problem By Using PHP to Replace Img Src From A String</h2>
Doing some research, I found that the ERP had a hook in which you could introduce custom logic before the ERP would sync the data to Big Commerce. Because each product description is stored as a string of HTML, I figured, I could manipulate the product description directly and add HTTPS to all images inside of the content.

This is the code I used to solve this problem:
<pre>&lt;?php

$productDesc = '&lt;a href="https://ua1.us/"&gt;Text&lt;/a&gt;';
$productDesc .= '&lt;img src="http://ua1.us/media/media.jpg"&gt;';
$productDesc .= '&lt;div class="test"&gt;&lt;/div&gt;';
$productDesc .= '&lt;img src="http://ua1.us/media/media1.jpg"&gt;';

preg_match_all('/&lt;img[^&gt;]+&gt;/i', $productDesc, $images);
foreach ($images[0] as $image) {
    $secureImg = str_replace('http://', 'https://', $image);
    $productDesc = str_replace($image, $secureImg, $productDesc);
}
echo $productDesc;</pre>
In the code above, what we are doing is using the <code>preg_match_all</code> method in PHP to return all instances of <code>&lt;img&gt;</code> tags as an array of the original image tags. We then loop through each image tag and use the <code>str_replace</code> method to find the <code>http://</code> and replace with the <code>https://</code>. This returns a string the represents a secured image request. We then repeat the find a replace. This time, we are looking to replace the original image with the secured one.

Originally Posted at [UA1 Labs](https://ua1.us/news/tutorials/tutorial-php-replace-img-src-from-string/)