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

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

报告相同问题?

悬赏问题

  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 请教一下c语言的代码里有一个地方不懂
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档