Remove the words having special characters from a string

Lets learn how to remove the words from a string which contains the special characters. I mean the word having non-English characters. Consider the below example.
asdasd óadsa, xxxÂr 123, asda koskso øppp iø1 asdakjd*ads8

You can see several words from the above contains special/non-english character on them. I'll write a regex that will remove those kind of words from the sentence above.

A PHP code:
$string = "asdasd óadsa, xxxÂr 123, asda koskso øppp iø1 asdakjd*ads8";
$string = preg_replace('/\S*[^a-z0-9A-Z\s,\.]+\S*/', '', $string);
print "$string\n";

Output: asdasd 123, asda koskso

A Perl code:
$string = "asdasd óadsa, xxxÂr 123, asda koskso øppp iø1 asdakjd*ads8";
$string =~ s/\S*[^a-z0-9A-Z\s,\.]+\S*//g;
print "$string\n";

Output: asdasd 123, asda koskso

You can use trim on the string, or replace multiple spaces with single one after that. Also you can add other characters like dot, comma inside the regex to distinguish the word.

Cheers!!!

Comments

Unknown said…
can show how to remove special character in mysql