Return the portion of a string before the first occurrence of a character in php

前端 未结 5 1469
予麋鹿
予麋鹿 2020-11-28 09:41

In PHP, what is the simplest way to return the portion of a string before the first occurrence of a specific character?

For example, if I have a string...

\"

5条回答
  •  半阙折子戏
    2020-11-28 10:23

    You could do this:

    $string = 'The quick brown fox jumped over the lazy dog';
    $substring = substr($string, 0, strpos($string, ' '));
    

    But I like this better:

    list($firstWord) = explode(' ', $string);
    

提交回复
热议问题