file_get_contents将所有特殊字符转换为菱形问号php

时间:2018-02-02 10:59:55

标签: php html laravel file

我正在项目中上传一个html文档,并使用file_get_contents()重新检索文件内容。

但每当出现某个引号或任何特殊字符时,它都会被转换为菱形问号。

以下是我的代码

    **$path = public_path().'/upload/'.$filename.'.'.$extension;
    $striped_content = file_get_contents($path);**

stripe_content的输出低于

<p class=MsoNormal align=right style='margin-top:0cm;margin-right:2.1pt;
margin-bottom:0cm;margin-left:0cm;margin-bottom:.0001pt;text-align:right;
line-height:10.8pt;mso-line-height-rule:exactly'><u style='text-underline:black'>
<span lang=EN-US style='font-size:9.5pt;font-family:"Times New Roman","serif";
mso-fareast-font-family:"Times New Roman";position:relative;top:.5pt;
mso-text-raise:-.5pt'>
<span style='mso-spacerun:yes'>�</span>
</span>
</p>

以上代码是简单的html由引号组成,但它显示问号而不是.. 我现在该怎么办。

4 个答案:

答案 0 :(得分:0)

file_get_contents不会转换任何内容,只需从URL返回数据即可。 出现在您的浏览器中,因为它不在浏览器使用的字符集中。

答案 1 :(得分:0)

您需要将path的字符编码转换为UTF-8

试试这个:

$path = public_path().'/upload/'.$filename.'.'.$extension;
$resolved_path = mb_convert_encoding($path, 'HTML-ENTITIES', "UTF-8");
$striped_content = file_get_contents($resolved_path);

答案 2 :(得分:0)

$context = stream_context_create(array(
'http'=>array(
    'method' => "GET",
    'header' =>  'Accept-Charset: UTF-8, *;q=0',
)));

$ striped_content = file_get_contents($ path,false,$ context);

答案 3 :(得分:0)

The Character displayed in black diamond question mark are treated as ISO-8859-1 unicode and we are converting it into utf-8.
Below Is my Code That worked Perfectly

$path = public_path().'/upload/'.$filename.'.'.$extension;
$striped_content = file_get_contents($path);
$striped_content = mb_convert_encoding($striped_content, 'HTML-ENTITIES', "ISO-8859-1");

我希望我的回答也会帮助其他人。