PHP: How can I remove from String the last word?

后端 未结 6 1247
轻奢々
轻奢々 2021-02-05 21:39

How can I remove, with PHP, the last word from a String?

For example the string \"Hi, I\'m Gian Marco\" would become \"Hi, I\'m Gian\".

6条回答
  •  半阙折子戏
    2021-02-05 22:17

    You can do it with regular expression. (see answer of Ahmed Ziani.)

    However, in PHP you can also do it using some inbuilt function. see the code below

    $text = "Hi, I'm Gian Marco";
    $last_space_position = strrpos($text, ' ');
    $text = substr($text, 0, $last_space_position);
    echo $text;
    

提交回复
热议问题