I tried this:
echo preg_replace('/[^,,$]/', '', ',test,hi,');
But gets:
,,,
I tried this:
echo preg_replace('/[^,,$]/', '', ',test,hi,');
But gets:
,,,
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,',');