I tried this:
echo preg_replace('/[^,,$]/', '', ',test,hi,');
But gets:
,,,开发者_如何学Python
Do you mean
preg_replace('/^,|,$/', '', ',test,hi,');
? Inside a character class […]
, a leading ^
means negation, and $
doesn't have any special meanings.
You could use the trim
function instead.
trim(',test,hi,', ',');
preg_replace is a bit overkill
$string = ',,ABCD,EFG,,,,';
$newString trim($string,',');
trim(',test,hi,',','); // echoes test,hi
精彩评论