Need to write a html output in a file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jayarajmohan
Forum Newbie
Posts: 16
Joined: Fri Aug 29, 2008 12:06 am

Need to write a html output in a file

Post by jayarajmohan »

Hi all,

I need to write a html output to the text file.

For ex.
<?php
$str = "<html><body><table><tr><td>fjlhfg</td><td>dsdsdf</td></tr></table></body></html>";

$myFile = "report.txt";
$fh = fopen($myFile, 'w') or die("can't open file");

echo $str;
fwrite($fh, $str);
fclose($fh);
?>

Here the out put of the report.txt is "<html><body><table><tr><td>fjlhfg</td><td>dsdsdf</td></tr></table></body></html>"

But while we run this in browser it shows like "fjlhfg dsdsdf"

I need to write the browser output to a file (as like fjlhfg dsdsdf)..

Any idea's welcome....

Thanks..
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Need to write a html output in a file

Post by jackpf »

I'm not entirely sure what you mean...but is it strip_tags() you want?
jayarajmohan
Forum Newbie
Posts: 16
Joined: Fri Aug 29, 2008 12:06 am

Re: Need to write a html output in a file

Post by jayarajmohan »

Yes you come near to my requirements.

strip_tags() function is exclude the html and php tags.

But i want the exact html out put as like displayed in browser.

Like :
$str = "abc<br>dfg";

echo $str;

// output is
abc
dfg

The same output i want to write in a text file. note like abcdfg (strip_tags this works like this)

I hope u got my point..

Thanks
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Need to write a html output in a file

Post by jackpf »

Well, you can't have complete html formatting in a text file.

There are functions you can use though...

To convert <br /> tags to new lines, you could do this:

Code: Select all

$str = str_replace('<br />', "\n", $str);
Stuff like that.
jayarajmohan
Forum Newbie
Posts: 16
Joined: Fri Aug 29, 2008 12:06 am

Re: Need to write a html output in a file

Post by jayarajmohan »

Okay thank you for your ideas...

JayaraJMohan.J
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Need to write a html output in a file

Post by alex.barylski »

It's displayed like in the browser because the tags are rendered...if I understand ylu correctly you maybe want htmlentities() so the text appears as full source code when viewed in the browser???
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Need to write a html output in a file

Post by Benjamin »

:arrow: Moved to PHP - Code
Post Reply