2

I would like to change all special characters in my string but I want to keep all accented ones. Is it possible with a preg_replace() ?

My current code:

preg_replace('/[^A-Za-z0-9\-]/', '', $string); 
HamZa
  • 14,671
  • 11
  • 54
  • 75
Dimitri
  • 922
  • 2
  • 13
  • 34

1 Answers1

5

Try Unicode:

preg_replace('/[^\p{L}0-9\-]/u', '', $string);

\p{L} is a Unicode property that matches all letters in any language, Unicode properties on php.net

stema
  • 90,351
  • 20
  • 107
  • 135
  • Hey stema ! Thanks you for your nice reply ! That's work fine ! If I wan to no replace the & and the single quote, how I can do that ? – Dimitri Jan 16 '14 at 11:50
  • The documentation for PHP seems more accurate for this question: http://www.php.net/manual/en/regexp.reference.unicode.php – Ignacio Lago Jan 16 '14 at 11:51
  • @IgnacioLago, thanks for that link, added it to the answer – stema Jan 16 '14 at 12:05
  • @Dimi, just add additional characters to your negated character class, e.g. `[/[^\p{L}0-9&\-]/]` – stema Jan 16 '14 at 12:06