开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜