How to remove non-alphanumeric characters without losing accentuation?

Asked

Viewed 2,118 times

3

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

However, in this way I am losing the accentuation of words, someone has some answer?

1 answer

5


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

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

  • \p{L} - letters

  • \p{N} - digits

  • \s - blank space

You can use it like this:

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

The result is:

Olá amanhã é dia 20
  • Reference: http://php.net/manual/en/regexp.reference.unicode.php

  • 1

    Help guy :) thanks a lot

  • I didn’t know that P

Browser other questions tagged

You are not signed in. Login or sign up in order to post.