Php trim string at a particular character

前端 未结 7 1354
花落未央
花落未央 2021-01-11 18:32

Is there a php string function to trim a string after a particular character. I had a look on the php.net site and did a google search but couldn\'t find anything. The only

7条回答
  •  失恋的感觉
    2021-01-11 19:00

    The strstr and stristr functions finds the first occurrence in a string and returns everything after it (including the search string). But it you supply true as the third argument, it brings back everything in front of the search string.

    $string = strstr( $string, '?', true); # Returns /gallery/image
    

    If the match is not found it returns FALSE so you could write an error check like this:

    if( $path = strstr( $string, '?', true) ){
       # Do something
    }
    

提交回复
热议问题