PHP 用 % 符号清理传入的字符串

发布于 2025-01-04 18:24:39 字数 612 浏览 2 评论 0原文

我在 php 字符串中有一个查询,我试图根据 % 符号进行过滤。 整个内容都是 utf-8 格式的。 如果字符串包含 % (例如 %acheron),它将转换为其等效实体(在我提到的情况下,它变成由字符串中的 %ac 序列解释的“not”符号字符)。 我似乎无法清理这个字符的传入字符串/ 我无法使用 php 替换来清理字符串,因为当它出现在脚本中时,它已经“解码了?”。

我发现我能弄清楚的唯一方法是使用 rawurlencode 然后清理它,但这似乎是一个蹩脚的解决方案,并且会给我带来其他字符的问题。

是否有一些更有效的方法来清除这些字符而不影响字符串的其余部分?例如我正在寻找一个可以剥离 % & 的函数# (以及任何其他)来自字符串,但否则保持完整。 (preg_replace 对我不起作用)

换句话说,有没有一种方法可以接受字符串并清理它,而无需转换任何潜在的特殊字符,以便我可以将它们从字符串中删除。

编辑:查询是通过 GET 传入的,对此尚不清楚。 EDIT2:使用 urlecode 或 rawurl 编码: %acheron 转换为 %ACheron (我猜它可以被清理)但是 ^acheron 被转换为 %5Eacheron (因此几乎不可能通过模式清理它)...

请原谅我的问题的愚蠢 谢谢 拉里

I have a query in a php string which i am trying to filter against % signs.
The whole thing is in utf-8.
If the string contains % (for example %acheron) it gets converted to its equivalent entity (in the case I mention it becomes the 'not' sign character which is interpreted by the %ac sequence in the string).
I cant seem to be able to clean the incoming string of this character/
I cant clean the string with a php replace because by the time its in the script it is already "decoded?".

The only way I found that I could figure out was to use rawurlencode and then clean it but it seems like a crappy solution and one that creates problems with other characters for me.

Is there some more efficient way to clearing these characters without affecting the rest of the string ? for instance I am looking for a function that would strip % & # (and any other) from the string, but leave it intact otherwise. (preg_replace is not working for me)

In other words is there a way to accept the string and clean it without any potentially special characters getting converted so that I can strip them from the string.

EDIT: The query is coming in via GET, wasn't clear on that.
EDIT2: With urlecode or rawurl encode :
%acheron is converted to %ACheron (which could be cleaned I guess) but
^acheron is converted to %5Eacheron (thus making it almost impossible to clean it via a pattern)...

Please excuse the noobishness of my question
Thanks
Larry

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

待天淡蓝洁白时 2025-01-11 18:24:39

$string = preg_replace("/(%|&|#)/", '', $string);

像这样的东西吗?如果我理解你的问题,这应该可行。

编辑:好的,这就是您要找的:

<?php
function myUrlEncode($string) {
    $entities = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D', '%5E');
    $replacements = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]", "^");
    return str_replace($entities, $replacements, urlencode($string));
}


$search = myUrlEncode($_GET['id']);
$search = preg_replace("/(%|&|#)/", '', $search);

echo $search;
?>

$string = preg_replace("/(%|&|#)/", '', $string);

Something like this? This should work, if I understood your problem.

EDIT: Ok this is what you're looking for:

<?php
function myUrlEncode($string) {
    $entities = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D', '%5E');
    $replacements = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]", "^");
    return str_replace($entities, $replacements, urlencode($string));
}


$search = myUrlEncode($_GET['id']);
$search = preg_replace("/(%|&|#)/", '', $search);

echo $search;
?>
ゃ人海孤独症 2025-01-11 18:24:39

在我看来, str_replace 就是您正在寻找的东西。

例如:

$text = "%acheron";
$search = "%";
$replace = ""; //or whatever you want to replace it with
echo str_replace($search, $replace, $text);

您还可以为 $search 创建一个数组,并分别为 $replace 创建一个数组

It sounds to me like str_replace is what you are looking for.

For example:

$text = "%acheron";
$search = "%";
$replace = ""; //or whatever you want to replace it with
echo str_replace($search, $replace, $text);

You can aswell make an array for $search and respectively an array for $replace

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