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

发布于 2024-10-27 08:02:56 字数 531 浏览 2 评论 0原文

当我通过此函数运行包含双引号的短语时,它会将引号替换为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

久伴你 2024-11-03 08:02:56

我不会调用该函数 string_sanitize(),因为它具有误导性。您可以将其称为 strip_non_alphanumeric()

您当前的函数将删除所有不是大写或小写字母或数字的内容。

您可以仅删除 '" ...

$str = str_replace(array('\'', '"'), '', $str); 

I would not call that function string_sanitize(), as it is misleading. You could call it 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); 
執念 2024-11-03 08:02:56

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

---编辑---

删除非字母数字字符的最简单方法可能是使用 html_entity_decode 解码 HTML 字符,然后运行它由于在这种情况下,您不会得到任何需要重新编码的内容,因此您不需要执行 htmlentities,但值得记住的是,您拥有 HTML 数据,并且现在拥有原始未编码数据。

例如:

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

请注意,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 string 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 had HTML data and you now have raw unencoded data.

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.".

梦亿 2024-11-03 08:02:56

我认为你的 preg_replace 调用应该是这样的:

$result = preg_replace("/[^a-zA-Z0-9]+/", "", html_entity_decode($s));

请参阅 html_entity_decode 参考< /a> 了解更多详细信息。

I think your preg_replace call should be like this:

$result = preg_replace("/[^a-zA-Z0-9]+/", "", html_entity_decode($s));

Please see html_entity_decode reference for more details.

抱猫软卧 2024-11-03 08:02:56

您的函数使用正则表达式来删除与 [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.

厌倦 2024-11-03 08:02:56

为了确保删除所有类型的引号(包括左侧与右侧不同的引号),我认为它必须是类似的;

function string_sanitize($s) {
    $result = htmlentities($s);
    $result = preg_replace('/^(")(.*)(")$/', "$2", $result);
    $result = preg_replace('/^(«)(.*)(»)$/', "$2", $result);
    $result = preg_replace('/^(“)(.*)(”)$/', "$2", $result);
    $result = preg_replace('/^(')(.*)(')$/', "$2", $result);
    $result = html_entity_decode($result);
    return $result;
}

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('/^(")(.*)(")$/', "$2", $result);
    $result = preg_replace('/^(«)(.*)(»)$/', "$2", $result);
    $result = preg_replace('/^(“)(.*)(”)$/', "$2", $result);
    $result = preg_replace('/^(')(.*)(')$/', "$2", $result);
    $result = html_entity_decode($result);
    return $result;
}
终陌 2024-11-03 08:02:56

单引号和双引号的简单方法:)
并且仍然留下类似的东西来看看。

$clean_string = str_replace('"', '``', str_replace("'", "`", $UserInput));

Easy way for both single and double quotes : )
And still leaves something similar to look at.

$clean_string = str_replace('"', '``', str_replace("'", "`", $UserInput));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文