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 145 views
2 answers 121 views
1 answer 101 views
3 answers 134 views
1 answer 101 views
...