grosseskino
grosseskino

Reputation: 299

Parse a string containing a URL then delimited numbers, then populate an array repeating the URL with those numbers

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]); ?>"/>

Upvotes: 1

Views: 831

Answers (3)

mickmackusa
mickmackusa

Reputation: 48041

You can pass the exploded string into preg_replace_callback() and cache the leading and trailing substrings from the first element to be used in subsequent elements. Demo

var_export(
    preg_replace_callback(
        '#(\D*)(\d+)(\D*)#',
        function($m) {
            static $url = $m[1];
            static $suffix = $m[3];
            return "$url{$m[2]}$suffix";
        },
        explode(',', $url)
    )
);

Or from PHP7.4 through PHP8.2: Demo

var_export(
    preg_replace_callback(
        '#(\D*)(\d+)(\D*)#',
        function($m) {
            static $url, $suffix;
            $url ??= $m[1];
            $suffix ??= $m[3];
            return "$url{$m[2]}$suffix";
        },
        explode(',', $url)
    )
);

Upvotes: 0

deefour
deefour

Reputation: 35370

$url   = 'http://example.com/111.jpg,121,122,123,124,125,126';
$parts = explode(',', $url);
$url   = array_shift($parts);

$parts_count = count($parts);
for($i=0; $i<$parts_count; $i++) {
  $array[] = preg_replace('/\d+.jpg/', sprintf("%d.jpg", $parts[$i]), $url);
}

var_dump($array);

outputs

array(6) {
  [0]=>
  string(26) "http://example.com/121.jpg"
  [1]=>
  string(26) "http://example.com/122.jpg"
  [2]=>
  string(26) "http://example.com/123.jpg"
  [3]=>
  string(26) "http://example.com/124.jpg"
  [4]=>
  string(26) "http://example.com/125.jpg"
  [5]=>
  string(26) "http://example.com/126.jpg"
}

Upvotes: 0

Marc B
Marc B

Reputation: 360812

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...

Upvotes: 2

Related Questions