如何从字符串中删除单引号和双引号

Sco*_*t B 10 php preg-replace

当我通过此函数运行包含双引号的短语时,它用quot替换引号.

我想完全删除它们(也是单引号).我怎样才能改变这个功能呢?

function string_sanitize($s) {
    $result = preg_replace("/[^a-zA-Z0-9]+/", "", $s);
    return $result;
}
Run Code Online (Sandbox Code Playgroud)

更新:

Example 1: This is 'the' first example 
returns: Thisis030the039firstexample 
Errors: Warning: preg_match_all() [function.preg-match-all]: Unknown modifier '0' in C


Example 2: This is my "second" example
returns: Thisismyquotsecondquotexample
Errors: Invalid express in Xpath
Run Code Online (Sandbox Code Playgroud)

ale*_*lex 29

我不会称之为该功能string_sanitize(),因为它具有误导性.你可以打电话给它strip_non_alphanumeric().

您当前的函数将删除任何不是大写或小写字母或数字的内容.

你可以只剥除'"用...

$str = str_replace(array('\'', '"'), '', $str); 
Run Code Online (Sandbox Code Playgroud)


Ham*_*ish 16

看起来您的原始字符串具有"(")的HTML字符,因此当您尝试对其进行清理时,您只需删除the,&;保留字符串的其余部分quot.

- -编辑 - -

删除非字母数字字符的最简单方法可能是使用html_entity_decode解码HTML字符,然后通过正则表达式运行它.因为,在这种情况下,你不会得到任何东西,需要重新编码,你不需要再做ヶ辆,但它是值得记住的是,你 HTML数据,你现在有原始未经数据.

例如:

function string_sanitize($s) {
    $result = preg_replace("/[^a-zA-Z0-9]+/", "", html_entity_decode($s, ENT_QUOTES));
    return $result;
}
Run Code Online (Sandbox Code Playgroud)

注意,ENT_QUOTES将函数标记为"...转换双引号和单引号".