如何更改PHP文件中文本的输出颜色

Test.txt文件包含一些文本。如何从PHP文件中更改文本的输出颜色。


Test.txt file

The output should be like this:

This is a | text (The word "text" should be red)

This is another | text (The word "text" should be red)

This is the | best (The word "best" should be red)


三国纷争
浏览 313回答 2
2回答

慕田峪9158850

你可以做到$text = file_get_contents('file.txt');$text = str_replace('text',"<span style='color:red;'>text</span>" , $text);echo $text;对于多项选择,请尝试此$text = file_get_contents('files.txt');$pattern = ['text','best'];$replacements = ["<span style='color:red;'>text</span>","<span style='color:red;'>best</span>"];$text = str_replace($pattern,$replacements , $text);echo $text;要更改|使用此后出现的字符串的颜色$fn = fopen("files.txt","r");while(! feof($fn))&nbsp; {&nbsp; &nbsp; $line = fgets($fn);&nbsp; &nbsp; $needle = substr($line, strpos($line, '|')+1, strlen($line));&nbsp; &nbsp; echo $line&nbsp; &nbsp;= str_replace($needle, "<span style='color:red;'>{$needle}</span>", $line);&nbsp; &nbsp;echo '<hr>';}fclose($fn);

哔哔one

您无法使用PHP更改文本颜色。您必须使用HTML文档上的CSS更改文本颜色。
打开App,查看更多内容
随时随地看视频慕课网APP