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

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) "uploads/20/thumbs/8470177001370850253.png"
string(40) "uploads/20/thumbs/967693821370850253.png"
string(41) "uploads/20/thumbs/8470177001370850253.png"
string(40) " uploads/20/thumbs/967693821370850253.png"

ライブデモ

正規表現の説明:

  • ^- 文字列はここから開始する必要があります
  • uploads/- リテラル文字列「uploads/」
  • [0-9]+- 0 から 9 までの数字が 1 回以上繰り返される
  • /- リテラル文字列「/」

に置き換えていuploads/{ID}/ます。

于 2013-07-10T08:03:54.980 に答える
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 に答える
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 で置き換えます

編集:

3で始まる文字列のみ動作するように変更

于 2013-07-10T08:28:24.117 に答える