PHP 修剪和空间不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18299896/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:18:26  来源:igfitidea点击:

PHP trim and space not working

phpencodingspacetrim

提问by Horse

I have some data imported from a csv. The import script grabs all email addresses in the csv and after validating them, imports them into a db.

我有一些从 csv 导入的数据。导入脚本获取 csv 中的所有电子邮件地址,并在验证它们后,将它们导入到数据库中。

A client has supplied this csv, and some of the emails seem to have a space at the end of the cell. No problem, trim that sucker off... nope, wont work.

一个客户提供了这个 csv,并且一些电子邮件似乎在单元格的末尾有一个空格。没问题,把那个笨蛋剪掉……不,行不通。

The space seems to not be a space, and isn't being removed so is failing a bunch of the emails validation.

该空间似乎不是一个空间,并且没有被删除,因此一堆电子邮件验证失败。

Question:Any way I can actually detect what this erroneous character is, and how I can remove it?

问题:有什么办法可以真正检测到这个错误字符是什么,以及如何删除它?

Not sure if its some funky encoding, or something else going on, but I dont fancy going through and removing them all manually! If I UTF-8 encode the string first it shows this character as a:

不确定它是否有一些时髦的编码,或者其他什么东西,但我不喜欢手动完成并删除它们!如果我首先对字符串进行 UTF-8 编码,它会将这个字符显示为:

?

?

回答by Sven

If that "space" is not affected by trim(), the first step is to identify it.

如果那个“空间”不受 影响trim(),那么第一步就是识别它。

Use urlencode()on the string. Urlencode will percent-escape any non-printable and a lot of printable characters besides ASCII, so you will see the hexcode of the offending characters instantly. Depending on what you discover, you can act accordingly or update your question to get additional help.

urlencode()在字符串上使用。Urlencode 将百分比转义除 ASCII 之外的任何不可打印和大量可打印字符,因此您将立即看到违规字符的十六进制代码。根据您的发现,您可以采取相应的行动或更新您的问题以获得更多帮助。

回答by Rid Iculous

I had a similar problem, also loading emails from CSVs and having issues with "undetectable" whitespaces.

我遇到了类似的问题,还从 CSV 加载电子邮件,并且遇到了“无法检测”空格的问题。

Resolved it by replacing the most common urlencoded whitespace chars with ''. This might help if can't use mb_detect_encoding() and/or iconv()

通过将最常见的 urlencoded 空白字符替换为 '' 来解决它。如果不能使用 mb_detect_encoding() 和/或 iconv(),这可能会有所帮助

    $urlEncodedWhiteSpaceChars   = '%81,%7F,%C5%8D,%8D,%8F,%C2%90,%C2,%90,%9D,%C2%A0,%A0,%C2%AD,%AD,%08,%09,%0A,%0D';
    $temp = explode(',', $urlEncodedWhiteSpaceChars); // turn them into a temp array so we can loop accross
    $email_address  = urlencode($row['EMAIL_ADDRESS']);
        foreach($temp as $v){
            $email_address  =  str_replace($v, '', $email_address);     // replace the current char with nuffink
        }
        $email_address = urldecode($email_address); // undo the url_encode

Note that this does NOT strip the 'normal' space character and that it removes these whitespace chars from anywhere in the string - not just start or end.

请注意,这不会去除“正常”空格字符,而是从字符串中的任何位置删除这些空白字符 - 不仅仅是开始或结束。

回答by Slipstream

In most of the cases a simple strip_tags($string)will work.

在大多数情况下,一个简单的strip_tags($string)就行了。

If the above doesn't work, then you should try to identify the characters resorting to urlencode()and then act accordingly.

如果上述方法不起作用,那么您应该尝试识别求助于的字符urlencode(),然后采取相应的行动。

回答by Mateusz Nowak

I see couples of possible solutions

我看到了几种可能的解决方案

1) Get last char of string in PHP and check if it is a normal character (with regexp for example). If it is not a normal character, then remove it.

1)在PHP中获取字符串的最后一个字符并检查它是否是正常字符(例如使用regexp)。如果它不是普通字符,则将其删除。

$length = strlen($string);
$string[($length-1)] = '';

2) Convert your character from UTF-8 to encoding of you CSV file and use str_replace. For example if you CSV is encoded in ISO-8859-2

2) 将您的字符从 UTF-8 转换为 CSV 文件的编码并使用 str_replace。例如,如果您的 CSV 编码为 ISO-8859-2

echo iconv('UTF-8', 'ISO-8859-2', "?");