1

数据

3   uploads/8/thumbs/8470177001370850253.png
3   uploads/10/thumbs/967693821370850253.png
3   uploads/9/thumbs/8470177001370850253.png
3   uploads/11/thumbs/967693821370850253.png

我的输入是 id="20" 现在我需要为每个 id 3 静态更改为“uploads/20/thumbs/967693821370850253.png”。我需要这样的输出..

3   uploads/20/thumbs/8470177001370850253.png
3   uploads/20/thumbs/967693821370850253.png
3   uploads/20/thumbs/8470177001370850253.png
3   uploads/20/thumbs/967693821370850253.png

我试过爆炸和内爆,但我不能改变它怎么办?我将从数据库中检索数据。

我的编码是

function albums_download($input, $serviceName) {
    $ipJson = json_encode($input);
    $this->db->select('photo_thumb_url,photo_url');
    $this->db->from('photos');
    $this->db->where('album_id', $input['album_id']);
    $query = $this->db->get();
    $result = $query->result();
    foreach ($query->result() as $row)
    {
         $data[] = $row->photo_thumb_url;
         $data[] = $row->photo_url;

         $explode_data = explode('/',$row->photo_thumb_url);
         $explode_data[1] = 20;
         $data['change'] = implode('/', $explode_data);

    }

        $status = $this->privue_lib->return_status('success', $serviceName, $data, $ipJson);

    return $status;

}

4

4 回答 4

2

说你的数据是 $data 数组

尝试这个

$data=array('uploads/8/thumbs/8470177001370850253.png',
      'uploads/10/thumbs/967693821370850253.png',
      'uploads/9/thumbs/8470177001370850253.png',
      'uploads/11/thumbs/967693821370850253.png');

foreach ($data as $row){
$temparray=explode('/',$row);
$temparray[1]=20;
echo implode('/',$temparray); 
}
于 2013-07-10T08:05:30.433 回答
2

我不知道 CodeIgniter,但您可以简单地使用正则表达式替换:

<?php
    $id = 20;

    $imagePaths = array(
        "uploads/8/thumbs/8470177001370850253.png",
        "uploads/10/thumbs/967693821370850253.png",
        "uploads/9/thumbs/8470177001370850253.png",
        "uploads/11/thumbs/967693821370850253.png"
    );

    foreach ($imagePaths as $imagePath) {
        $newImagePath = preg_replace("#^uploads/[0-9]+/#", "uploads/" . $id . "/", $imagePath);
        var_dump($newImagePath);
    }
?>

会输出:

字符串(41)“上传/20/thumbs/8470177001370850253.png”
字符串(40)“上传/20/thumbs/967693821370850253.png”
字符串(41)“上传/20/thumbs/8470177001370850253.png”
字符串(40)“上传/20/thumbs/967693821370850253.png"

现场演示

正则表达式的解释:

  • ^- 字符串必须从这里开始
  • uploads/- 文字字符串“上传/”
  • [0-9]+- 0 到 9 之间的数字重复一次或多次
  • /- 文字字符串“/”

我们将其替换为uploads/{ID}/.

于 2013-07-10T08:03:54.980 回答
0

您可以使用简单的正则表达式

$images = array(
    "3 uploads/8/thumbs/8470177001370850253.png",
    "3 uploads/10/thumbs/967693821370850253.png",
    "3 uploads/9/thumbs/8470177001370850253.png",
    "4 uploads/9/thumbs/8470177001370850253.png",
    "3 uploads/11/thumbs/967693821370850253.png"
);

foreach ($images as &$im)
{
    if(substr($im, 0, 1) == '3')
    $im = preg_replace("#/[0-9]+/#",  "/20/", $im);
}
print_r($images);

输出:

Array ( 
       [0] => 3 uploads/20/thumbs/8470177001370850253.png 
       [1] => 3 uploads/20/thumbs/967693821370850253.png 
       [2] => 3 uploads/20/thumbs/8470177001370850253.png 
       [3] => 4 uploads/9/thumbs/8470177001370850253.png 
       [4] => 3 uploads/20/thumbs/967693821370850253.png
      )

/[0-9]+/在 / 和 / 之间至少查找 1 次数字并将其替换为/20/foreach 使用引用,因此它可以替换循环中变量的值,然后使用 print_r 打印它

编辑:

更改为仅适用于以 3 开头的字符串

于 2013-07-10T08:28:24.117 回答
0

试试这个,假设路径都是一致的。

$explode_data = explode('/', 'uploads/8/thumbs/8470177001370850253.png');
$explode_data[1] = 20;
$implode_data = implode('/', $explode_data);

这将返回

uploads/20/thumbs/8470177001370850253.png
于 2013-07-10T08:04:10.053 回答