-2

I have a file main.php with code snippet as

<pre><code>

echo htmlspecialchars ('echo "this is a $name of your best friend";'); 

</pre></code>

When I run main.php file in browser I get

echo "this is a $name of your best friend";

This output is okay.

I want to write this output to a text file - output.txt

The problem is if I try to write the content to a output.txt , I get code snippet between tags rather than output. It seems I have to run file in browser. But even after running file in browser, the content can be seen but can't be written to output.txt

How I can do it. It would be best if I can write to output.txt without opening main.php file in browser.

$fp = fopen('output.txt', 'w'); fwrite($fp, $snippet); fclose($fp);

The problem is snippet has those pre code tags. I could not figure out what code could write rendered output to output.txt

Please note: This question is not about how to write to a file.

It's about writing rendered output to a file.

  • 1
    Does this answer your question? [How to write into a file in PHP?](https://stackoverflow.com/questions/1768894/how-to-write-into-a-file-in-php) – Martin Zeitler May 14 '22 at 03:13
  • $fp = fopen('output.txt', 'w'); fwrite($fp, $snippet); fclose($fp); The problem is snippet has those pre code tags. I could not figure out what code could write rendered output to output.txt – Manish Srivastava May 14 '22 at 03:14
  • Please edit the question with the code you are trying. – Chris Haas May 14 '22 at 03:35

1 Answers1

0

Actually this is not possible in direct way. The reason is that client side rendered values can't be taken into db or used outside. What I mean, suppose you have a value calculated on client side ( like javascript giving value in id) . To make a file from rendered html is only way is to curl the url in backend and then write the content to a file.