我试过了:
echo preg_replace('/[^,,$]/', '', ',test,hi,');
但得到:
,,,
答案 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