Remove everything except letters from PHP string

2020-02-01 18:42发布

问题:

How can I remove everything from a string apart from letters? I have an input field for first names.

回答1:

In PHP, you can use (as suggested by @rczajka and @mario):

preg_replace('/\PL/u', '', $str)

Working Example: http://codepad.viper-7.com/V78skl

You may want to checkout this Tutorial for regex



回答2:

$new_string = preg_replace('/[^a-z]/i','',$old_string);


回答3:

$new_string = ereg_replace("[^A-Za-z]", "", $string );