[ACCEPTED]-Regex to enforce alpha-numeric but allow apostrophes and hyphens?-regex

Accepted answer
Score: 11

Just include the single apostrophe and the 1 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;
}
Score: 2

Trying to get an answer to you as quick 5 as I can.

I think you can just add them into 4 the character class; possibly you'll need 3 to escape them though.

Note: to use the - be 2 sure to put it at the end otherwise it will 1 be considered a meta character.

More Related questions