问题
I am looking to replace all characters in a string except letters, numbers, spaces and underscores.
Could someone please provide a example?
回答1:
I normally use something like:
$string = preg_replace("/[^ \w]+/", "", $string);
That replaces all non-space and non-word characters with nothing.
回答2:
[^0-9a-zA-Z_\s]
is what you want to replace.
回答3:
<?php
$string = 'April 15, 2003';
$pattern = '/[^\w ]+/';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
?>
来源:https://stackoverflow.com/questions/6442174/replace-all-characters-except-letters-numbers-spaces-and-underscores