开发者

How to remove the probable dot at the beginning/end of string in PHP?

开发者 https://www.devze.com 2023-01-05 13:41 出处:网络
I tried this: echo preg_replace(\'/[^,,$]/\', \'\', \',test,hi,\'); But gets: ,,,开发者_如何学Python

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
0

精彩评论

暂无评论...
验证码 换一张
取 消