0

My software uses the following before performing a search on a mySQL database:

$keywords_search = preg_replace("/[^a-zA-Z0-9 ]/", "", $keywords_search);

The problem is that it's stripping out words that users may use in other languages, like "españa" (spanish) due to the "ñ" character which is very common.

Is there any way to allow certain special characters in preg_replace?

user2643870
  • 965
  • 1
  • 10
  • 19

2 Answers2

0

If you want to make sure your keyword does not contain any malicious code, that's not a way to go, you should read this:

How can I prevent sql injection in php

If you just want to filter your search phrase, you can use the \p{L} pattern to match any letter and \p{N} to much any numeric character. Also you should use u modifier like this: /\p{L}+/u

Also be sure to check this question:

Regular expression \p{L} and \p{N}

Community
  • 1
  • 1
Adam Zielinski
  • 2,774
  • 1
  • 25
  • 36
0

You can try with this one

$keywords_search = preg_replace("/[^\w-\p{L}\p{N}\p{Pd}]/", "", $keywords_search);

This will match anything that's NOT an alphanumeric character (including UTF-8 letters) as well as the dash (-).

Suvash sarker
  • 3,140
  • 1
  • 18
  • 21
  • dash is included in `\p{Pd}` (not sure for underscore) and `\w` is included in `[\pL\PN]` so it becomes : `[^\pL\pN\p{Pd}_]` – Toto Sep 26 '13 at 14:32