PHP去除字串開頭或末尾逗號

2021-07-29 23:42:02 字數 1092 閱讀 9691

去除最後乙個逗號:

$str = "a,b,c,d,";

echo rtrim($str, ",");

返回的結果就是:a,b,c,d

去除開頭逗號:

$str = ",a,b,c,d";

echo ltrim($str, ",");

返回的結果就是:a,b,c,d

用到的函式:

trim() 函式從字串的兩端刪除空白字元和其他預定義字元。

trim(string,charlist)

string 必需。規定要檢查的字串。

charlist 可選。規定要轉換的字串。如果省略該引數,則刪除以下所有字元:

"\0" - null

"\t" - tab

"\n" - new line

"\x0b" - 縱向列表符

"\r" - 回車

" " - 普通空白字元

rtrim() 函式從字串的末端開始刪除空白字元或其他預定義字元。

string 必需。規定要轉換的字串。

charlist 可選。規定從字串中刪除哪些字元。如果未設定該引數,則全部刪除以下字元:

"\0" - ascii 0, null

"\t" - ascii 9, 製表符

"\n" - ascii 10, 新行

"\x0b" - ascii 11, 垂直製表符

"\r" - ascii 13, 回車

" " - ascii 32, 空格

ltrim() 函式從字串左側刪除空格或其他預定義字元。

string 必需。規定要轉換的字串。

charlist 可選。規定從字串中刪除哪些字元。如果未設定該引數,則全部刪除以下字元:

"\0" - ascii 0, null

"\t" - ascii 9, 製表符

"\n" - ascii 10, 新行

"\x0b" - ascii 11, 垂直製表符

"\r" - ascii 13, 回車

" " - ascii 32, 空格

PHP去除字串開頭或末尾逗號

去除最後乙個逗號 str a,b,c,d,echo rtrim str,返回的結果就是 a,b,c,d 去除開頭逗號 str a,b,c,d echo ltrim str,返回的結果就是 a,b,c,d 用到的函式 trim 函式從字串的兩端刪除空白字元和其他預定義字元。trim string,ch...

去掉字串首尾逗號 PHP去除字串開頭或末尾逗號

去除最後乙個逗號 str a,b,c,d,echo rtrim str,返回的結果就是 a,b,c,d 去除開頭逗號 str a,b,c,d echo ltrim str,返回的結果就是 a,b,c,d 用到的函式 trim 函式從字串的兩端刪除空白字元和其他預定義字元。trim string,ch...

notepad 每行末尾或開頭插入指定字串

需求 最近弄到乙個sql指令碼,如下 insert into student id,name values 1 張三 insert into student id,name values 2 李四 insert into student id,name values 3 王五 insert into...