Wednesday, September 26, 2018

php - how to know if a $string ends with ','?











hi there?




how can i know if the last char of a $string is ',' ?



thanks a lot?


Answer



There are a few options:



if (substr($string, -1) == ',') {



Or (slightly less readable):



if ($string[strlen($string) - 1] == ',') {


Or (even less readable):



if (strrpos($string, ',') == strlen($string) - 1) {



Or (even worse yet):



if (preg_match('/,$/', $string)) {


Or (wow this is bad):



if (end(explode(',', $string)) == '') {



The take away, is just use substr($string, -1) and be done with it. But there are many other alternatives out there...


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...