【问题标题】:Replace Array value with other preg_replaced value用其他 preg_replaced 值替换数组值
【发布时间】:2017-04-24 01:10:42
【问题描述】:

我有一个数组,我知道它的值是来自某个地方的 JPG

我需要转到返回到该数组的每个值并 preg_replace 一些字符

然后将返回值的值设置为其他值

这是代码,这是我尝试过的

//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??!

第一段代码适用于所有值,替换工作正常。

第二段代码是我的问题,它返回旧的未修改图像的值,我没有

我需要在写入图像之前对其进行修改 '$product->imageUrl & $product->subImageUrl'

【问题讨论】:

    标签: php arrays preg-replace


    【解决方案1】:

    问题很简单。您已经在修改数据 将其存储在$data['images']。要解决这个问题,只需将此行移至 在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);
    }
    

    【讨论】:

    • 谢谢伙计,正处于开始破坏东西的边缘:D
    猜你喜欢
    • 2019-02-06
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 2014-02-06
    • 1970-01-01
    相关资源
    最近更新 更多