1

I am using preg_replace to validate first and last name coming from my database. If I don't include preg_replace, the values are passed on like normal, but if I do use it, nothing is passed. Here is my current code:

$firstname = preg_replace("/^[A-Za-z]+$/", "", $firstname);
$lastname = preg_replace("/^[A-Za-z]+$/", "", $lastname);

I am using similar validation for other variables without issue, it's really only happening for these. I'm not too familiar with validation, so any help would be appreciated.

Eric Brown
  • 1,312
  • 3
  • 15
  • 30

2 Answers2

0

You need to use /[^A-Za-z]/ regex, this will filter out all except A-Z and a-z

$string = '09test'; 
$new_string = preg_replace("/[^A-Za-z]/",'',$string);
echo $new_string; //prints test
Ambrish Pathak
  • 3,813
  • 2
  • 15
  • 30
0

Try /[^A-Za-z]/g. Note the global modifier at the end. You can check on https://regex101.com/.

But I recommend you transliterate your non English chars to English Transliterate any convertible utf8 char into ascii equivalent In this case you won't lose data for example in my name which is János, but you'll get Janos instead of Jnos

Community
  • 1
  • 1
jkrnak
  • 956
  • 6
  • 9