开发者

Regex to enforce alpha-numeric but allow apostrophes and hyphens?

开发者 https://www.devze.com 2023-02-25 06:11 出处:网络
How would I alter this function so that it allows single apostrophe\'s an开发者_StackOverflowd 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

精彩评论

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