views:

78

answers:

4

Hi everyone...

I have a website displaying data from MySQL in a php file (/something.php).... i want to copy the source code of that page as HTML so i can use it in a textfield box so users can copy paste that code...

It's almost like an HTML generator using info from mySQL, so users can custimize each HTML code.

I have everything covered... except the display HTML thing.

Please Help

A: 

You need to escape the HTML into HTML entities. For instance, convert < into &lt;.

Yann Ramin
A: 

There's a php function htmlspecialchars you should look into.

For rendering check either eval (quick & dirty) or ob_start and friends (the more complex way to do it, though safer and generally supported by more hosters).

Jakub Hampl
i'm using it... <? echo . htmlspecialchars(file_get_contents(__FILE__)) .; ?>but it's displaying the original php file... not the result of the query as html
AlexGuz
OK, you might want to make clear in the question that you want to **render** the php and then display the *resulting* html.
Jakub Hampl
A: 

You need to actually request the page from the web server, not simply read its contents in order for the PHP to execute and produce the result. That is, if I understand correctly that PHP (the file you were simply reading) is querying the database to actually fetch the desired HTML to display.

So, something like (if permitted) file_get_contents("http://url_of_php_file_you_were_simply_reading_not_requesting"); , then of course run that through htmlspecialchars();

Better to just use CURL to stay portable when requesting the page.

Tim Post
+1  A: 
echo htmlspecialchars(eval(file_get_contents('path/to/your/file')));

Eval is generally frowned upon however but this is a quick and easy solution.

Lotus Notes
now display this error<b>Warning</b>: file_get_contents(FILE) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: No such file or directory in <b>/home/
AlexGuz
Change FILE to your filename, of course.
Lotus Notes