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 279 views
2 answers 232 views
1 answer 181 views
3 answers 275 views
1 answer 190 views
1 answer 171 views
...