Remove everything after a character in PHP

后端 未结 8 2785
无人共我
无人共我 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条回答
  •  旧时难觅i
    2021-02-19 08:39

    substr and strpos

    The simplest way to do this is with substr() DOCs and strpos() DOCs.

    $string = 'test?=new';
    $cut_position = strpos($string, '?') + 1; // remove the +1 if you don't want the ? included
    $string = substr($string, 0, $cut_position);
    

    As you can see substr() extracts a sub-string from a string by index and strpos() returns the index of the first instance of the character it is searching for (in this case ?).

提交回复
热议问题