Regex to enforce alpha-numeric but allow apostrophes and hyphens?
How would I alter this function so that it allows single apostrophe's an开发者_StackOverflowd hyphens:
function sanitize_string($s) {
$result = preg_replace("/[^a-zA-Z0-9]+/", " ", html_entity_decode($s, ENT_QUOTES));
return $result;
}
Just include the single apostrophe and the hyphen at the end of the range:
function sanitize_string($s) {
$result = preg_replace("/[^a-zA-Z0-9'-]+/", " ", html_entity_decode($s, ENT_QUOTES));
return $result;
}
Trying to get an answer to you as quick as I can.
I think you can just add them into the character class; possibly you'll need to escape them though.
Note: to use the -
be sure to put it at the end otherwise it will be considered a meta character.
精彩评论