w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Preg match and Preg replace specific format

I'm sure if your allowing whitespace in the username, you can suffice with just a space character but to be safe use s which matches whitespace ( , , , f, and " "), for that you can just add that inside of your character class []

if (preg_match('/^[a-zA-Z0-9s]{5,12}+$/u', $username)) { ...

And you can leave your preg_replace() function as is...

Update: To match different characters, but not mixed you could try the following:

$user = 'hwדגדשרביd';   // "invalid"
$user = 'fooo';         //  "valid"
$user = 'שדגדשרביב';   //  "valid"

if (preg_match('/^[p{Hebrew}]{2,10}|[a-zA-Z]{2,10}$/u', $user)) {
  echo "valid";
} else {
  echo "invalid";

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.