remove everything after first comma from string in php

后端 未结 5 2073
闹比i
闹比i 2020-12-31 21:13

I want to remove everything(including the comma) from the first comma of a string in php eg.

$print=\"50 days,7 hours\";

should become \"50

5条回答
  •  情歌与酒
    2020-12-31 21:34

    You can also use current function:

    $firstpart = current(explode(',', $print)); // will return current item in array, by default first
    

    Also other functions from this family:

    $nextpart = next(explode(',', $print)); // will return next item in array
    
    $lastpart = end(explode(',', $print)); // will return last item in array
    

提交回复
热议问题