php 如何从字符串中删除单引号和双引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5454273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove single and double quotes from a string
提问by Scott B
When I run a phrase that contains double quotes through this function, its replacing the quotes with quot.
当我通过此函数运行包含双引号的短语时,它会将引号替换为 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
回答by alex
I would not call that function string_sanitize()
, as it is misleading. You could call it strip_non_alphanumeric()
.
我不会调用该函数string_sanitize()
,因为它具有误导性。你可以称之为strip_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...
你可以只剥除'
和"
用...
$str = str_replace(array('\'', '"'), '', $str);
回答by Hamish
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 string quot
.
看起来您的原始字符串具有"
( "
)的 HTML 字符,因此当您尝试对其进行清理时,您只需删除&
and ;
,留下字符串的其余部分quot
。
---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 hadHTML data and you now have raw unencodeddata.
删除非字母数字字符的最简单方法可能是使用html_entity_decode解码 HTML 字符,然后通过正则表达式运行它。因为,在这种情况下,您将不会得到任何需要重新编码的内容,因此您不需要执行htmlentities,但值得记住的是,您拥有HTML 数据,而现在拥有未编码的原始数据。
Eg:
例如:
function string_sanitize($s) {
$result = preg_replace("/[^a-zA-Z0-9]+/", "", html_entity_decode($s, ENT_QUOTES));
return $result;
}
Note that ENT_QUOTES
flags the function to "...convert both double and single quotes.".
请注意,ENT_QUOTES
将函数标记为“...转换双引号和单引号。”。
回答by anubhava
I think your preg_replace call should be like this:
我认为你的 preg_replace 调用应该是这样的:
$result = preg_replace("/[^a-zA-Z0-9]+/", "", html_entity_decode($s));
Please see html_entity_decode referencefor more details.
有关更多详细信息,请参阅html_entity_decode 参考。
回答by Stnfordly
Easy way for both single and double quotes : ) And still leaves something similar to look at.
单引号和双引号的简单方法:) 仍然留下类似的东西。
$clean_string = str_replace('"', '``', str_replace("'", "`", $UserInput));
回答by gramo
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;
为了确保删除所有类型的引号(包括左侧与右侧不同的引号),我认为它必须是这样的;
function string_sanitize($s) {
$result = htmlentities($s);
$result = preg_replace('/^(")(.*)(")$/', "", $result);
$result = preg_replace('/^(«)(.*)(»)$/', "", $result);
$result = preg_replace('/^(“)(.*)(”)$/', "", $result);
$result = preg_replace('/^(')(.*)(')$/', "", $result);
$result = html_entity_decode($result);
return $result;
}
回答by Hoàng Long
Your function uses regular expression to remove any character that different from [a-zA-Z0-9], so it surely removes any "" or ''
您的函数使用正则表达式删除与 [a-zA-Z0-9] 不同的任何字符,因此它肯定会删除任何 "" 或 ''
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_decodefirst.
编辑:好吧,从 Hamish 的回答我意识到你的字符串是一个 HTML 字符串,所以它解释了为什么“(") 被转换为“quot”。你可以考虑先用"e
preg_replace 或htmlspecialchars_decode替换。