如何从字符串中删除单引号和双引号
当我通过此函数运行包含双引号的短语时,它会将引号替换为 quot。
我想完全删除它们(也是单引号)。我怎样才能改变函数来做到这一点?
function string_sanitize($s) {
$result = preg_replace("/[^a-zA-Z0-9]+/", "", $s);
return $result;
}
更新:
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
When I run a phrase that contains double quotes through this function, its replacing the quotes with quot.
I want to completely remove them (also single quotes). How can I alter the function to do that?
function string_sanitize($s) {
$result = preg_replace("/[^a-zA-Z0-9]+/", "", $s);
return $result;
}
Update:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我不会调用该函数
string_sanitize()
,因为它具有误导性。您可以将其称为strip_non_alphanumeric()
。您当前的函数将删除所有不是大写或小写字母或数字的内容。
您可以仅删除
'
和"
...I would not call that function
string_sanitize()
, as it is misleading. You could call itstrip_non_alphanumeric()
.Your current function will strip anything that isn't an upper or lowercase letter or a number.
You can strip just
'
and"
with...看起来您的原始字符串包含
"
("
) 的 HTML 字符,因此当您尝试对其进行清理时,只需删除& ;
和;
,保留字符串quot
的其余部分---编辑---
删除非字母数字字符的最简单方法可能是使用 html_entity_decode 解码 HTML 字符,然后运行它由于在这种情况下,您不会得到任何需要重新编码的内容,因此您不需要执行 htmlentities,但值得记住的是,您拥有 HTML 数据,并且现在拥有原始未编码数据。
例如:
请注意,
ENT_QUOTES
将函数标记为“...转换双引号和单引号。”。It looks like your original string had the HTML characters for
"
("
) so when you attempt to sanitize it, you're simply remove the&
and;
, leaving the rest of the stringquot
.---EDIT---
Probably the easiest way to remove non alpha numeric characters would be to decode the HTML characters with html_entity_decode, then run it through the regular expression. Since, in this case, you won't get anything that needs to be re-coded, you don't need to then do htmlentities, but it's worth remembering that you had HTML data and you now have raw unencoded data.
Eg:
Note that
ENT_QUOTES
flags the function to "...convert both double and single quotes.".我认为你的 preg_replace 调用应该是这样的:
请参阅 html_entity_decode 参考< /a> 了解更多详细信息。
I think your preg_replace call should be like this:
Please see html_entity_decode reference for more details.
您的函数使用正则表达式来删除与 [a-zA-Z0-9] 不同的任何字符,因此它肯定会删除任何“”或“
编辑:好吧,从哈米什的回答我意识到您的字符串是一个 HTML 字符串,因此它解释了为什么“(")要转换为“quot”。您可以考虑用preg_replace替换
"e
,或者htmlspecialchars_decode。Your function uses regular expression to remove any character that different from [a-zA-Z0-9], so it surely removes any "" or ''
EDIT: well, from Hamish answer I realize your string is a HTML string, so that it explain why "(") to be transformed to "quot". You may consider replace
"e
by preg_replace, or htmlspecialchars_decode first.为了确保删除所有类型的引号(包括左侧与右侧不同的引号),我认为它必须是类似的;
In order to be sure of remove all kind of quotes (including those into which left side are different from the right side ones) I think it must be something like;
单引号和双引号的简单方法:)
并且仍然留下类似的东西来看看。
Easy way for both single and double quotes : )
And still leaves something similar to look at.