I have several thousand posts in my wp site which have hard coded URLs to images on various domain names (live URL, development URL, UAT url etc). For example:
<img src="https://prod.domain.com/wp-content/image-path.jpg"
srcset="https://dev.domain.com/wp-content/image-path.jpg,
https://dev.domain.com/wp-content/image-path.jpg,
https://another.different-domain.com/wp-content/image-path.jpg">
<img src="https://another.different-domain.com/wp-content/image-path.jpg"
srcset="https://another.different-domain.com/wp-content/image-path.jpg,
https://another.different-domain.com/wp-content/image-path.jpg,
https://another.different-domain.com/wp-content/image-path.jpg">
I need to be able to search and replace any host from the string (full html tag) with a specific host. Basically redirecting everything to the CDN. At a later date Ill clean up the DB.
I’ve tried the following regex in a regex generator online which seems to produce the matches I need. However, when I take this to code it doesnt work and the $new_html_tag variable is null.
https://regex101.com/r/zePZRO/1
$filtered_image = /*html as per regex demo*/
$new_html_tag = preg_replace('(?<=https://).*?(?=/wp-content)', 'https://new-domain.com', $filtered_image);