0

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'

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.