Remove everything after a character in PHP

后端 未结 8 2705
无人共我
无人共我 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:34

    You can do this with a well-written regex, but the much simpler and quicker way to do it is to explode the string on the "?" character, and use the first element in the resulting array.

    $str = "test?=new";
    $str2 = explode("?", $str);
    $use_this = $str2[0];
    

    $use_this[0] will be "test". If you want to add the "?" back, just concatenate:

    $use_this = $use_this."?";
    

提交回复
热议问题