1

i am trying to echo a html files contents from a php script. Quite simple implementation

$response=file_get_contents("second.html"); echo $response; 

When i hit the html file directly all the content seems ok but when i get the echoed string it is different, The html contains javascript with utf8 strings and escape characters. It seems that "echo" is changing the string because i tried saving the $response string in a second html file and that appears ok also. My question is, is there a way to just plainly give the string with no processing.

Also it seems the echoed html works on firefox, but not on chrome, the file html works on both.

8
  • Are you calling a local file? Commented Jun 20, 2012 at 0:12
  • whats the encoding of the 2 pages? Commented Jun 20, 2012 at 0:12
  • echo is not adding javascript or escape characters. Are you working inside of a framework? Commented Jun 20, 2012 at 0:12
  • 2
    You can combine those two lines into one by doing: readfile('second.html');. Commented Jun 20, 2012 at 0:18
  • I can try combining but i will need to process the html later on so that cant be an option. Commented Jun 20, 2012 at 0:24

3 Answers 3

1

What do you get if you do:

var_export(get_magic_quotes_gpc()); 

Is it on? (true)

If so:

http://www.php.net/manual/en/security.magicquotes.disabling.php

or

$response = stripslashes(file_get_contents("second.html")); 
Sign up to request clarification or add additional context in comments.

1 Comment

Magic quotes are on, and the second does not work, because as i said the string is read perfectly, i tried a file_put_contents before the echo and it was ok and different from the echoed string.
0

Could be that you're not telling the browser what encoding you're using?

header('Content-Type: text/html; charset=utf-8'); 

1 Comment

I'm in the same case. The header was already there and plain text is showing. Going to try suggestions above.
0

If I understand right, you want to view the HTML as plain text, you can run a PHP script to find and replace the "<" and ">" characters to HTML entities using 'str_replace()'. HTML Entities

Then when you echo, PHP handles them as "<" and ">" in plain text not HTML at ALL.

Learn more about find and replace in PHP using str_replace():

str_replace PHP Tutorial
Example:

<?php $response = file_get_contents("second.html"); str_replace("<", "&lt;",$response); str_replace(">", "&gt;",$response; echo $response; ?> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.