What must occur is that the +
represents space, so when using the $_GET['q']
it contains space, not the symbol of +
. This exchange, internal PHP, makes the str_replace
does not work, because there is no +
at that point.
For your code to work you must replace the +
for %2B
, then if you have:
https://site.com/q=1+2+3
Switch to:
https://site.com/q=1%2B2%2B3
Your code will work because the %2B
is the hexadecimal code for +
, in ASCII, it will not be converted to space by PHP.
To convert automatically you can use the http_build_query
, following RFC3986, if specified:
echo 'https://site.com?' . http_build_query(['q' => '1+2+3'], '', '&', PHP_QUERY_RFC3986);
For example. This will result in the URL mentioned above, which will work.
and the content of
q
comes from where?– Don't Panic