premtech11 premtech11 - 3 months ago 18
PHP Question

How to remove part of a string after last comma in PHP

How to remove part of a string after last comma in PHP ?

String :

"this is a post, number 1, date 23, month 04, year 2012"


Expected:
"this is a post, number 1, date 23, month 04"

Answer

substr and strrpos would be useful

$until = substr($string, 0, strrpos($string.",", ","));

Note: edited based on comments below

Comments