preg_replace('/\?(.*?)/i', '', 'http://site.ru/?lang=eng&type=set');
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 ...
preg_replace('/\?(.*?)/i', '', 'http://site.ru/?lang=eng&type=set');
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 ...
It is possible through explode =).
$url = 'http://site.ru/?lang=eng&type=set'; $a = explode('?', $url); $a = $a[0]; echo $a;
I will consider your expression so that you understand why it does not work.
/\?(.*?)/i
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.
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!
Source: https://ru.stackoverflow.com/questions/130845/
All Articles