在codeigniter中爆炸和内爆

时间:2013-07-10 07:58:53

标签: php codeigniter

数据

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 个答案:

答案 0 :(得分: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);
    }
?>

输出:

  

string(41)&#34;上传/ 20 / thumbs / 8470177001370850253.png&#34;
  string(40)&#34;上传/ 20 / thumbs / 967693821370850253.png&#34;
  string(41)&#34;上传/ 20 / thumbs / 8470177001370850253.png&#34;
  string(40)&#34;上传/ 20 / thumbs / 967693821370850253.png&#34;

Live demo

正则表达式的解释:

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

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

答案 1 :(得分:2)

说你的数据是$ data array

试试这个

$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); 
}

答案 2 :(得分:0)

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

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

这将返回

uploads/20/thumbs/8470177001370850253.png

答案 3 :(得分: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开头的字符串