Ahmed Nabil
Ahmed Nabil

Reputation: 53

Replace Array value with other preg_replaced value

i've got an array, which i know that its values would be JPGs from somewhere

i need to go to each value returned to that array and preg_replace some characters

then set the values of the returned values to some other value

here's the code and here's what i've tried

//first piece of code
$data['images'] = array();
        foreach ($single['PictureURL'] as $ispec) {

            $data['images'][] = $ispec;
            $ispec = preg_replace('/\$_[0-9]+.JPG\b/i', '$_10.JPG', $ispec); 
            $file = 'C:\wamp64\www\mz\images1.txt';
            file_put_contents ($file, $ispec, FILE_APPEND);
//images1.txt shows all images returned fine with modified strings
          }

//second piece of code
            $product->imageUrl = $data['images'][0];
            unset($data['images'][0]);
            $product->subImageUrl = $data['images'];
                $file = 'C:\wamp64\www\mz\images3.txt';
            file_put_contents ($file, $data['images'], FILE_APPEND);
//images3.txt shows all the images returned but without being modified?? WHY??!

the first piece of the code is working on all values and replacing is working just fine.

the second piece of the code is my issue, it is returning the values of the old none modified images, which i don't

i need to modify the images before its being written to '$product->imageUrl & $product->subImageUrl'

Upvotes: 0

Views: 26

Answers (1)

sidyll
sidyll

Reputation: 59297

The problem is very simple. You're modifying your data after you already stored it in $data['images']. To solve this, just move this line to after the preg_replace:

foreach ($single['PictureURL'] as $ispec) {
    $ispec = preg_replace('/\$_[0-9]+.JPG\b/i', '$_10.JPG', $ispec); 
    $data['images'][] = $ispec;
    $file = 'C:\wamp64\www\mz\images1.txt';
    file_put_contents ($file, $ispec, FILE_APPEND);
}

Upvotes: 1

Related Questions