drcomwc134525 2017-04-24 01:10
浏览 15
已采纳

将Array值替换为其他preg_replaced值

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条回答 默认 最新

  • dongou0524 2017-04-24 01:22
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?