PHP 用 % 符号清理传入的字符串
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样的东西吗?如果我理解你的问题,这应该可行。
编辑:好的,这就是您要找的:
Something like this? This should work, if I understood your problem.
EDIT: Ok this is what you're looking for:
在我看来, str_replace 就是您正在寻找的东西。
例如:
您还可以为
$search
创建一个数组,并分别为$replace
创建一个数组It sounds to me like str_replace is what you are looking for.
For example:
You can aswell make an array for
$search
and respectively an array for$replace