How to remove the leading and trailing commas from a string in PHP

2 Answers

0 votes
$string = ",,,,PHP,,,";

$string = trim($string, ",");

echo $string; 



/*
run:

PHP

*/

 



answered Mar 6, 2025 by avibootz
0 votes
$string = ",,,,PHP,,,";

$string = preg_replace('/^,+|,+$/', '', $string);

echo $string; 



/*
run:

PHP

*/

 



answered Mar 6, 2025 by avibootz

Related questions

3 answers 297 views
2 answers 239 views
1 answer 186 views
3 answers 284 views
1 answer 196 views
1 answer 174 views
...