wamp
wamp

Reputation: 5949

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

I tried this:

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

But gets:

,,,

Upvotes: 0

Views: 1177

Answers (3)

Romain Deveaud
Romain Deveaud

Reputation: 824

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

Upvotes: 1

Mark Baker
Mark Baker

Reputation: 212402

preg_replace is a bit overkill

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

Upvotes: 4

kennytm
kennytm

Reputation: 523184

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,', ',');

Upvotes: 7

Related Questions