How can I remove everything from a string apart from letters? I have an input field for first names.
$result = preg_replace("/[^a-zA-Z0-9]+/", "", $s); If your definition of alphanumeric includes letters in foreign languages and obsolete scripts then you will need to use the Unicode character classes. Try this to leave only A-Z: $result = preg_replace("/[^A-Z]+/", "", $s);
Using str_replace() Method: The str_replace() method is used to remove all the special characters from the given string str by replacing these characters with the white space (” “).
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
$new_string = preg_replace('/[^a-z]/i','',$old_string);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With