在preg_replace中允许“ - ”和“。”

$username = preg_replace("/[^\p{L}|\p{N}]/u", "", html_entity_decode($username));

This allows "letters" and "numbers" in the $username, removing everything else.

But how can I also allow "." dot symbols in the mix? also "-" would be nice.

Thank you very much!

Just add those into your character class:

$var = 'Василий. Теркин-';
$username = preg_replace('/[^\p{L}\p{N}.-]/u', "", $var);
var_dump($username); // Василий.Теркин-

Demo. I have removed |, as it obviously is covered by the character class already (and obviously shouldn't be used in a character class as an alternation - after all, class IS an alternation). )

Escape them with a \

I don't get the rest of your regex honestly but that's how you use special characters.

The regex I would use is: [a-z0-9\-\.] (add the case insensitive flag).

. has no meaning in a character class, so doesn't need escaping. - has no meaning either IF it is the first or last character.

$username = preg_replace("/[^a-z0-9.-]/u","",html_entity_decode($username));