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 ö
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:
< ö
to:
< &ouml;
It will display in textarea:
< ö
After you submit form, it will send to server text
< ö
not:
< &ouml;
Tested in Chrome with jQuery:
$('#layout').html('<form method="POST"><textarea name="x"><&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):
<a href="https://www.w3schools.com">Go to w3schools.com</a>
The browser output of the code above will be:
<a href="https://www.w3schools.com">Go to w3schools.com</a>