How to remove non-alphanumeric characters without losing accent?

3

I'm able to remove the non-alphanumeric as follows preg_replace('/[^a-z0-9\s]/i', null, $string );

But in this way I'm losing the accentuation of words, does anyone have an answer?

    
asked by anonymous 02.11.2014 / 00:55

1 answer

5

I'm not sure what you're trying to filter but looking for digits and letters should work fine:

[^\p{L}\p{N}\s] means everything that is not:

  • \p{L} - letters

  • \p{N} - digits

  • \s - blank space

You can use this:

$string = 'Olá amanhã é dia! #20%';
$limpa = preg_replace('/[^\p{L}\p{N}\s]/', '', $string );
echo $limpa;

The result is:

Olá amanhã é dia 20
    
02.11.2014 / 01:19