I'm trying to replace image names within a page by using preg_replace but I'm probably missing something with regular expressions.
For the sake of the example, I'll use the following page: https://www.laurentwillen.be/circuits/circuit-autriche/alpbach/
So here is what I do:
$media = get_attached_media( 'image', $metadata["page_id"] );
foreach ($media as $key=>$value)
{
$old_image = explode("/",$media[$key]->guid);
$old_image = $old_image[sizeof($old_image)-1];
$old_image = explode(".",$old_image);
$old_image = "/".$old_image[0]."/";
$new_image = wp_get_attachment_image_src($key,'tablet');
$new_image = explode ("/",$new_image[0]);
$new_image = $new_image[sizeof($new_image)-1];
$new_image = explode (".",$new_image);
$new_image = "/".$new_image[0]."/";
preg_replace($old_image,$new_image,$content,-1,$count);
}
To spare you the trouble of guessing the variable values, here is what $old_image and $new_image represent for the page given higher:
$old_image values:
/alpbach-photo-1/
/alpbach-photo-2/
/alpbach-photo-3/
/alpbach-photo-4/
/alpbach-photo-5/
/alpbach-photo-6/
/alpbach-photo-top/
$new_image values:
alpbach-photo-1-900x600
alpbach-photo-2-900x600
alpbach-photo-3-900x600
alpbach-photo-4-900x600
alpbach-photo-5-900x600
alpbach-photo-6-900x600
alpbach-photo-top-900x281
The count for preg_replace returns
9
0
9
0
9
0
0
But in the end, nothing is replaced.
If I try manually this:
preg_replace("/alpbach-photo-3/","/alpbach-photo-1/",$content,-1,$count);
Nothing is replaced either.
What am I doing wrong? Any idea?
Thanks!