preg_replace('/\?(.*?)/i', '', ''); 

How to remove all that after the sign " ? "? In this case, only the sign itself is deleted, but everything that comes after it is necessary ...

    4 answers 4

    It is possible through explode =).

     $url = ''; $a = explode('?', $url); $a = $a[0]; echo $a; 
    • @Node_pro, thanks! - ModaL

    I will consider your expression so that you understand why it does not work.


    1. (Major error) After the educational sign is the minimum quantification.


    The minimum quantifier works as follows: it tries to capture the minimum value, and expands to the right only if it is necessary for a general match. In your case, after it costs nothing, which means it just coincides with “nothing” always.
    That is, this expression will always find only the first question mark.
    2. (Unnecessarily) You use grouping brackets for


    this is unnecessary, since then this group is not used in the replacement expression.
    3. (Not at all) You use the ignore flag of the case of characters, but there is not a single character in the expression.
    4. (How to fix) To correct an error, you must replace the minimum quantification with the maximum one. Such a quantifier at first captures the maximum possible text, and reduces its size only if it is necessary for general coincidence, but if after it nothing has to match, then it will capture all the text to the end of the line. That is


    regular expression successfully cope with the task.

    • @ReinRaus, wow, thank you very much for such an explanation! - ModaL

    There is also a function parse_url, meaning it should be more suitable.

       $var = 'Hello, world!?123123123'; echo stristr($var,'?',true); // >= php ver 5.3 

      will output Hello, world!

      php stristr

      • A description should be added to what is happening - Ep1demic
      • Please add a description of the code to your reply to make it easier for users to understand. To edit the answer, click Edit . You can also read tips on how to write a good answer . - From the queue of checks - Yuri