Hello! In fact the file_get_contents()
does exactly what you want!
From the PHP manual: file_get_contents()
- Reads all contents of a file to a string.
That is to say: file_get_contents()
does not interpret the file, just reads back and forth exactly what is written on it, without processing anything; returning exactly the contents of the file when read.
The problem: when you perform the echo
in the rescued code, it ends up anyway being printed on the page, that is, directly in the HTML markup of the document and so the browser will interpret the source code you got, in case it will read the tags.
The solution: in fact you’re doing everything right, soon, no need for solution. If you want to read the source code on the page, you have to use printable characters, ie, exchange markup code "< > &
" for "< > &
". Luckily PHP does this with a native function htmlentities()
.
About the htmlentities()
:
<?php
$str = "<© W3Sçh°°¦§>";
echo htmlentities($str);
?>
The HTML printed on the page will be:
<!DOCTYPE html>
<html>
<body>
<© W3Sçh°°¦§>
</body>
</html>
In the browser, it will be interpreted and displayed on the page as:
<© W3Sçh°°¦§>
More about the htmlentities()
:
The
file_get_contents
is giving you the source, it only appears formatted because you are givingecho
.– bfavaretto
It’s like @bfavaretto says.
– Jorge B.