I am having some issues with PHP preg_replace.
I have a url that contains a image-url with numbers added, e.g.:
$url = http://example.com/111.jpg,121,122,123,124,125,126
The numbers at the end are always different.
To separate the string, I am using
$parts = explode(",", $url);
To figure out how many numbers there are, I am using:
$numbers = count($parts);
My problem is to replace the end of $url[0] with $parts (starting with parts[1] up to parts[$numbers-1])
Any idea what I need to change??
Here is my code:
for ($i = 1; $i <= 10; $i++) {
$array[] = preg_replace('/\d+.jpg/', sprintf("%01d.jpg", $i), $url[0]);
}
<img src="<?php echo($array[0]); ?>"/>
<img src="<?php echo($array[1]); ?>"/>
<img src="<?php echo($array[2]); ?>"/>
<img src="<?php echo($array[3]); ?>"/>
<img src="<?php echo($array[4]); ?>"/>
<img src="<?php echo($array[5]); ?>"/>
<img src="<?php echo($array[6]); ?>"/>
<img src="<?php echo($array[7]); ?>"/>
<img src="<?php echo($array[8]); ?>"/>
<img src="<?php echo($array[9]); ?>"/>
Try
$array[] = preg_replace('/\d+.jpg/', "{$url[$i]}.jpg"), $url[0]);
This'll pull out the trailing numbers one at a time. Your original version was replacing with your loop counter, which almost surely were NOT going to be the same as the trailing numbers.
Yours is generating
http://example.com/001.jpg
http://example.com/002.jpg
etc...
and you want
http://example.com/121.jpg
http://example.com/122.jpg
etc...