如何删除PHP中字符串开头/结尾的可能点?

时间:2010-07-01 08:45:05

标签: php regex string

我试过了:

echo preg_replace('/[^,,$]/', '', ',test,hi,');

但得到:

,,,

3 个答案:

答案 0 :(得分:7)

你的意思是

preg_replace('/^,|,$/', '', ',test,hi,');

?在字符类[…]中,前导^表示否定,$没有任何特殊含义。

您可以使用trim function代替。

trim(',test,hi,', ',');

答案 1 :(得分:4)

preg_replace有点矫枉过正

$string = ',,ABCD,EFG,,,,';
$newString trim($string,',');

答案 2 :(得分:1)

trim(',test,hi,',','); // echoes test,hi