php 从字符串中去除隐藏字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14654153/
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
Strip hidden character from string
提问by Bill H
This is something that should be simple but I can't figure out.
这应该很简单,但我无法弄清楚。
The site in question is UTF-8 encoded.
有问题的网站是 UTF-8 编码的。
A customer has been having trouble filling out a form on our website. Here is example data they have entered.
一位客户在填写我们网站上的表格时遇到问题。这是他们输入的示例数据。
SPICER-SMITHS LOST
斯派塞-史密斯迷失
It looks like a regular string, but when you copy that string into an app like notepad++ you'll see a "?" appear in the word "SMITHS" ("SMITH?S").
它看起来像一个普通的字符串,但是当你将该字符串复制到像 notepad++ 这样的应用程序中时,你会看到一个“?” 出现在单词“SMITHS”(“SMITH?S”)中。
The script sanitizes the field and goes the extra step of removing the following characters:
"\r\n", "\n", "\r", "\t", "\0", "\x0B".
该脚本清理该字段并执行删除以下字符的额外步骤:
"\r\n", "\n", "\r", "\t", "\0", "\x0B".
It's not catching this hidden character though.
不过,它并没有抓住这个隐藏的角色。
Does anybody know what's going on here?
有人知道这里发生了什么吗?
EDIT: I'm using php. Here is the function that I use to sanitize the field:
编辑:我正在使用 php。这是我用来清理字段的函数:
function strip_hidden_chars($str)
{
$chars = array("\r\n", "\n", "\r", "\t", "$chars = array("\r\n", '\n', '\r', "\n", "\r", "\t", "##代码##", "\x0B");
str_replace($chars,"<br>",$data);
", "\x0B");
$str = str_replace($chars," ",$str);
return preg_replace('/\s+/',' ',$str);
}
EDIT 2: @thaJeztah led me to the answer. The string I was testing was the output from our support ticket after the customer had copied and pasted it from whatever application she is using. The actual input was
编辑 2:@thaJeztah 引导我找到答案。我正在测试的字符串是在客户从她正在使用的任何应用程序中复制和粘贴它之后我们的支持票的输出。实际输入是
SPICER-SMITH'S
斯派塞-史密斯
采纳答案by thaJeztah
You may try to have a look here; remove control characters?
你可以试试看这里;删除控制字符?
回答by Mikael HOUNDEGNON
this also work as well
这也有效
##代码##
