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

php string invisible character after get_file_contents()

我有一个 .txt 文件是从交换服务器的管理 shell 生成的。

我正在使用 php 并想使用 get_file_contents() 来获取文件的内容,然后使用 preg_match() 和 "DisplayName"。此问题适用于整个文件。

$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]);

如何删除这些不可见的字符或者它可能是什么? php 中是否有函数可以找出这个字符可能是什么?

https://www.soscisurvey.de/tools/view-chars.php表示没有隐藏字符。

示例:

显示名称:名称

服务器名称:服务器

主 Smtp 地址:电子邮件

电子邮件地址:{电子邮件列表}

希望大家能帮帮我。

看起来文件编码为 Unicode,而您希望它是纯 ASCII。

试试这个:

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

或自定义函数:

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");

感谢您的帮助,我是这样解决的:

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