Vahid Amiri
Vahid Amiri

Reputation: 11117

Appending to string each time a substring is found

I have a string that is made of html srcset. I extracted the value of srcset using DOMDocument, it's something like:

$srcset = "/wp-content/uploads/2016/05/something-970x647-c-300x190.jpg 300w, 
/wp-content/uploads/2016/05/something-970x647-c-120x76.jpg 120w, 
/wp-content/uploads/2016/05/something-970x647-c-265x168.jpg 265w";

As you can see, urls are relative. I want to change them into absolute form with the host being something like http://example.com

For that I was thinking of looping through occurrences of /wp-content in the string and append the host url to its beginning.

How do I do that?

Upvotes: 1

Views: 27

Answers (1)

Pedro Lobito
Pedro Lobito

Reputation: 98921

$srcset = str_replace('/wp-content/', 'http://example.com/wp-content/', $srcset);

Upvotes: 2

Related Questions