简体   繁体   English

get_file_contents()之后的php字符串不可见字符

[英]php string invisible character after get_file_contents()

I have a .txt file generated from the management shell of an exchange server. 我有一个从Exchange服务器的管理外壳生成的.txt文件。

I am using php and want to use get_file_contents() to get the content of the file and then a preg_match() with "DisplayName". 我正在使用php,并希望使用get_file_contents()来获取文件的内容,然后使用带有“ DisplayName”的preg_match()。 This problem applies to the whole file. 此问题适用于整个文件。

$file = file_get_contents("file.txt");

//Does not work
preg_match("/DisplayName/", $file, $matches);

//Does work
preg_match("/D.i.s.p.l.a.y.N.a.m.e/", $file, $matches);

//Returns 1
preg_match("/D(.)i/", $file, $matches);
echo strlen($matches[1][0]);

How do I remove these invisible characters or what could it be? 如何删除这些看不见的字符,或者可能是什么? Is there a function in php to find out what this character might be? php中是否有一个函数来查找此字符可能是什么?

https://www.soscisurvey.de/tools/view-chars.php says there are no hidden characters. https://www.soscisurvey.de/tools/view-chars.php表示没有隐藏的字符。

Example: 例:

DisplayName : Name DisplayName:名称

ServerName : Server ServerName:服务器

PrimarySmtpAddress : Email PrimarySmtpAddress:电子邮件

EmailAddresses : {Email list} 电子邮件地址:{电子邮件列表}

I hope you guys are able to help me. 我希望你们能帮助我。

Looks like the file is encoded as Unicode where you expect it to be plain ASCII. 看起来文件被编码为Unicode,而您希望它是纯ASCII。

Try this: 尝试这个:

$file = file_get_contents("file.txt", FILE_TEXT);

or a custom function: 或自定义函数:

function file_get_contents_utf8($fn) {
     $content = file_get_contents($fn);
      return mb_convert_encoding($content, 'UTF-8',
          mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
}

$file = file_get_contents_utf8("file.txt");

Thanks for your help, I was able to fix it like this: 感谢您的帮助,我能够像这样修复它:

$file = file_get_contents("file.txt");
$file = str_replace(chr(0), "", $file);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM