php file_get_contents()转换html实体,如ö 到ö

I try to display the contents of a php file in a textarea with

<textarea ...>
<?php echo file_get_contents("file.php"); ?>
</textarea>

but it converts the html entities like &ouml; to ö. As it is php code I can not use a conversion like htmlspecialchars because it would brick the <> and quotes etc.

EDIT:

I checked the sourcecode with the browser and there is really an ö.

Please help, thank you!

htmlspecialchars will change:

< &ouml;

to:

&lt; &amp;ouml;

and it's ok.

It will display in textarea:

< &ouml;

After you submit form, it will send to server text

< &ouml;

not:

&lt; &amp;ouml;

Tested in Chrome with jQuery:

$('#layout').html('<form method="POST"><textarea name="x">&lt;&amp;ouml;</textarea><input type="submit" /></form>')

I think there is some non-UTF-8 character in your file.php. Try this:

     $content = file_get_contents('/tmp/file.php');
     $content = mb_convert_encoding($content, 'UTF-8',mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true)); 
     print_r($content);

You can refer this comment http://php.net/manual/en/function.file-get-contents.php#85008.

<textarea ...>
<?php echo htmlentities(file_get_contents("file.php")); ?>
</textarea>

Example

Convert some characters to HTML entities:

<?php
$str = '<a href="https://www.w3schools.com">Go to w3schools.com</a>';
echo htmlentities($str);
?>

The HTML output of the code above will be (View Source):

&lt;a href=&quot;https://www.w3schools.com&quot;&gt;Go to w3schools.com&lt;/a&gt;

The browser output of the code above will be:

<a href="https://www.w3schools.com">Go to w3schools.com</a>