PHP 筆記 rtrim function
最近在組合字串要塞到DB的時候, 無意間搜到一個很便利的FUNCTION來移除字串的最後一個字元. (參考網址: rtrim)範例:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$ary = array(/*假設這個陣列的資料要塞到資料庫*/); | |
//我們用implode來把資料組合成字串 | |
$ins_str = "(" . implode(' , ' , $ary) . " ), "; | |
//這時, 組合完的字串最後面會多一個豆號, 直接塞到QUERY | |
//的話會出現ERROR, 所以我們要把它去掉. | |
$ins_str = rtrim($ins_str, ","); | |
//這樣字串的豆號就會被去掉了, 比用SUBSTR方便 | |
$query = "INSERT into Table VALUES". $ins_str; | |
//以下就是執行DB的動作了 |
$str = rtrim($str, ',');
echo $str;
// (123456789) 逗號被去掉了
沒有留言:
張貼留言