Remove everything after a character in PHP

后端 未结 8 2763
无人共我
无人共我 2021-02-19 08:09

can any tell how to remove characters after ? in php. I have one string test?=new i need to remove the characters as well as = from that string.

相关标签:
8条回答
  • 2021-02-19 08:47

    Here is one-liner:

    $s = strpos($s, '?') !== FALSE ? strtok($s, '?') : $s;
    

    You can test it by the following command-line:

    php -r '$s = "123?456"; $s = strpos($s, "?") !== FALSE ? strtok($s, "?") : $s; echo $s;'
    
    0 讨论(0)
  • 2021-02-19 08:49

    Why not:

    $pos = strpos($str, '?'); // ? position
    $str = substr($str, 0, $pos);
    
    0 讨论(0)
提交回复
热议问题