0

I'm testing preg_quote() function in search function in Laravel. I found out a strange behavior - "+" is not escapes, but remove from the output.

preg_quote("a + b"); // "a b"

Tried to call same function without laravel staff from cli

php test.phpa // a \+ b

Seems Laravel makes some affect on function/output/something else...

Laravel 5.7. / php 7.3.1

Denys Siebov
  • 198
  • 4
  • 16
  • Can you show us your search function, how you output it, and to where? A `+` sign in a URL is a space in the resulting `$_GET` parameter and would need to be encoded as `%2B`. – ceejayoz Mar 04 '19 at 02:24
  • I'm using postman to test. request: `http://localhost:8000/api/users?skip=0&search=a + b` function first line: `dd(preg_quote($request->search));` The main problem is that "+" is the only one special character which is not escaped. I've tested with a string with all special chars from function doc. – Denys Siebov Mar 04 '19 at 02:31
  • @ceejayoz for this request `. \ + * ? [ ^ ] $ ( ) { } = ! < > | : - ` I've got this output ` \. \\ \* \? \[ \^ \] \$ \( \) \{ \} \= \! \< \> \| \: \- ` - everything is escaped, "+" ignored / removed. – Denys Siebov Mar 04 '19 at 02:34

1 Answers1

1

I'm using postman to test. request: http://localhost:8000/api/users?skip=0&search=a + b function first line: dd(preg_quote($request->search));

As I suspected, this isn't a problem with PHP or Laravel, but that your URL parameters should be urlencoded. (This'll happen if you're GETing via a <form> element, or by building a URL using Laravel's built-in URL builder, but if you're making a manual Postman request it's up to you.)

A + in a URL's query string corresponds to a space on the server. It must be escaped as %2B, as must characters like #. You can do this via urlencode.

http://localhost:8000/api/users?skip=0&search=a%20%2B%20b

ceejayoz
  • 176,543
  • 40
  • 303
  • 368