0

With this function I can validate numbers and letters but I need to pass them the accents, the "ñ" and comma "," and blank spaces. PHP

I would like something like this:

$str = 'Caballeros Díaz, Caballeros Bilingüe';

$result= preg_replace('/[^A-Za-z0-9\-]/', '', $str);
 //echo $result;

CaballerosDazCaballerosBilinge

allow this ñ áéíóú " , " numbers (spanish letters)

Carlos
  • 572
  • 1
  • 5
  • 13
  • 2
    Possible duplicate of https://stackoverflow.com/questions/2133758/how-do-i-match-accented-characters-with-php-preg Answers in this post might also help: https://stackoverflow.com/questions/3371697/replacing-accented-characters-php – Kay Aug 04 '18 at 01:13
  • thanks, just i needed undertnn consept, just need add the letters allow, like this: `/[^A-Za-z0-9 .,áéíóúüÁÉÍÓÚÜñÑ\-]/` – Carlos Aug 04 '18 at 01:57
  • 1
    Possible duplicate of [How do I match accented characters with PHP preg?](https://stackoverflow.com/questions/2133758/how-do-i-match-accented-characters-with-php-preg) – Jeff Meatball Yang Aug 04 '18 at 02:49
  • These kind of questions tend to have really vague specs. Does it really need to be specific about Spanish? Do you explicitly need to strip e.g. Catalan characters like `Ç` or `Ŀ` so `Barça` becomes `Bara`? Also, is you application using UTF-8? – Álvaro González Aug 04 '18 at 17:34

1 Answers1

0

I haven't tested this but this might work:

$str = 'Caballeros Díaz, Caballeros Bilingüe';

$result = preg_replace("/[^[:alnum:][:space:]]/u", '', $str);

See: http://php.net/manual/en/regexp.reference.character-classes.php

Jason Grim
  • 413
  • 4
  • 7