preg_replace('/[^a-zA-Z0-9]+/', '_', $sentence)
Basically it looks for any sequence of non-alphanumeric characters and replaces it with a single '_'. This way, you also avoid having two consecutive _'s in your output.
If it's for URLs, you probably also want them to be lower-case only:
preg_replace('/[^a-z0-9]+/', '_', strtolower($sentence))
preg_replace('/[^a-zA-Z0-9]+/', '_', $sentence)
Basically it looks for any sequence of non-alphanumeric characters and replaces it with a single '_'. This way, you also avoid having two consecutive _'s in your output.
If it's for URLs, you probably also want them to be lower-case only:
preg_replace('/[^a-z0-9]+/', '_', strtolower($sentence))
$a = preg_replace("/[^A-Za-z0-9]+/", "_", $str);
or /\W+/ if you want to keep everything that is considered a "letter" in the current locale
after replacement it may be also neccessary to stip leading and trailing underscores
$a = trim($a, '_');
I would build a regex (character class, to be exact) using your whitelisted characters, and then remove any character which matches the negation of that class.
$allowed_char_array = array("a","b","c","d","e") // and others
$chars = implode("", $allowed_char_array);
$regex = "/[^" . $chars . "]/u";
$input = "imageЙ ййé.png";
echo $regex . "\n";
$output = preg_replace($regex, "_", $input);
echo $input . "\n" . $output;
imageЙ ййé.png
image_ __é.png
If the above be not clear, here is what the actual all to preg_replace would look like:
preg_replace("/[^abcdefghijklmnopqrstuv]/u, "_", $input);
That is, any non whitelisted character would be replaced with just underscore. I did not bother to list out the entire character class, because you already have that in your source code.
Note that the /u flag in the regex is critical here, because your input string is a UTF-8 string. UTF-8 characters may consist of more than one byte, and using preg_replace on them without /u may have unexpected results.
You will want to use mb_strtolower() to convert multibyte characters to lowercase safely.
My solution uses strtr() to convert your French accented letters to your preferred form.
Since all characters are lowercased from the onset, you can halve your white list of French characters.
Using pathinfo() helps you to dissect your filename.
Code: (Demo)
$word = 'imageЙ ййé.png';
$parts = pathinfo($word);
$filename = strtr(mb_strtolower($parts['filename']), ['é' =>'é', 'à' => 'à','è' => 'è']);
echo preg_replace('~[^ a-zéàè]~u', '_', $filename) , "." , $parts['extension'];
Output:
image_ __é.png
You're almost there already:
$str = "Zebo's [Test]";
echo preg_replace("~['.!,;:@#$%^&*|()?/\\<> \t\r\n\[\]]~", "_", $str);
Output: Zebo_s__Test_
Edited to include [, ], and ' properly - didn't realize you meant that you wanted to replace those.
By the way... You say you want to replace "all special characters," and that your list above is just an "example." You may want to do something broader, like this:
preg_replace("~[^A-Za-z0-9]~", "_", $str);
This would also catch characters like the backtick and other special characters, such as:
`îõ§¶þäô
You can use:
$repl = preg_replace('~[.!,;:@#$%^&*|()?/\\\<>]~', '_', $str);
Put _ and . to the negated set of characters ([^...]):
$string = preg_replace('/[^a-zA-Z0-9_.]/', '', $string);
You should not omit $string = .. because preg_replace return replaced string. It does not change the string in place.
You can use some php filter widget like Purifier (to set a whitelist for input)...
But Still, we would like to suggest you to learn regex!
$newstr = preg_replace('/[^a-zA-Z0-9\']/', '_', "There wouldn't be any");
$newstr = str_replace("'", '', $newstr);
I put them on two separate lines to make the code a little more clear.
Note: If you're looking for Unicode support, see Filip's answer below. It will match all characters that register as letters in addition to A-z.
If you by writing "non letters and numbers" exclude more than [A-Za-z0-9] (ie. considering letters like åäö to be letters to) and want to be able to accurately handle UTF-8 strings \p{L} and \p{N} will be of aid.
\p{N}will match any "Number"\p{L}will match any "Letter Character", which includes- Lower case letter
- Modifier letter
- Other letter
- Title case letter
- Upper case letter
Documentation PHP: Unicode Character Properties
$data = "Thäre!wouldn't%bé#äny";
$new_data = str_replace ("'", "", $data);
$new_data = preg_replace ('/[^\p{L}\p{N}]/u', '_', $new_data);
var_dump (
$new_data
);
output
string(23) "Thäre_wouldnt_bé_äny"
You can use the array_map function.
function modify($str) {
return ucwords(str_replace("_", " ", $str));
}
Then in just use the above function as follows:
$states=array_map("modify", $old_states)
Need to use array_map function like as
$state = array("gujarat","andhra_pradesh","madhya_pradesh","uttar_pradesh");
$state = array_map(upper, $state);
function upper($state){
return str_replace('_', ' ', ucwords($state));
}
print_r($state);// output Array ( [0] => Gujarat [1] => Andhra pradesh [2] => Madhya pradesh [3] => Uttar pradesh )