0

I want to write a file using php,where some text are colored.

example:

operation: save. status:passed //its color green

operation:delete. status:failed //its color red

My code is like:

<?php
  $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
  $txt = "operation: save. status:passed\n";//green color
  fwrite($myfile, $txt);
  $txt = "operation:delete. status:failed\n";//red color
  fwrite($myfile, $txt);
  fclose($myfile);
?>
2
  • 1
    In txt files there are no ways to have different colors of text. Use html or something else. Commented Jan 11, 2015 at 10:18
  • @U-MULDER:)Thanks your suggestion. Commented Jan 11, 2015 at 10:19

1 Answer 1

2

There is no way to write "colored text" into a plain text file. That format simply does not support any such feature. A plain text file only supports plain text.

You would have to use another format, something like html markup or rich text to be able to specify a color. Such files can certainly be written in php just like any other file. But you have to use the correct syntax as defined by the target format.

I suggest you create such a file manually (using a text editor or similar). When the outcome suits your expectations then you return to your php code and write exactly that content into the opened target file. Note: you have to write the raw content, so the markup, not just plain text.

Sign up to request clarification or add additional context in comments.

7 Comments

output show console mode.so it's possible to use html
What do you mean by "console mode"? HTML markup certainly does not get rendered in a typical console like in 'terminal emulation' (read: shell), if that is what you mean.
text file show in console mode(linux terminal).(command: cat abc.txt)
OK, sure you can show that, but you cannot use html markup then. you'd have to use the terminal escape sequences in that case. For an example take a look at typical bash prompt definitions that often offer colored tokens inside the prompt.
Google offers endless examples. Here is one: tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html Or this one here on SO: stackoverflow.com/questions/10466749/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.