I know this question has been asked several times for sure, but I have my problems with regular expressions… So here is the (simple) thing I want to do in PHP:
I want to make a function which replaces unwanted characters of strings. Accepted characters should be:
a-z A-Z 0-9 _ – + ( ) { } # äöü ÄÖÜ space
I want all other characters to change to a “_”. Here is some sample code, but I don’t know what to fill in for the ?????:
<?php
// sample strings
$string1 = 'abd92 s_öse';
$string2 = 'ab! sd$ls_o';
// Replace unwanted chars in string by _
$string1 = preg_replace(?????, '_', $string1);
$string2 = preg_replace(?????, '_', $string2);
?>
Output should be:
$string1: abd92 s_öse (the same)
$string2: ab_ sd_ls_o
I was able to make it work for a-z, 0-9 but it would be nice to allow those additional characters, especially äöü. Thanks for your input!
To allow only the exact characters you described:
To allow all whitespace, not just the (space) character:
To allow letters from different alphabets — not just the specific ones you mentioned, but also things like Russian and Greek, or other types of accent marks:
If I were you, I’d go with the last one. Not only is it shorter and easier to read, but it’s less restrictive, and there’s no particular advantage to blocking stuff like
иifäöüÄÖÜare all fine.