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)
答案 0 :(得分:0)
您无法使用PHP更改文本颜色。您必须使用HTML文档上的CSS更改文本颜色。
答案 1 :(得分:0)
您可以按照以下方式操作
$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)) {
$line = fgets($fn);
$needle = substr($line, strpos($line, '|')+1, strlen($line));
echo $line = str_replace($needle, "<span style='color:red;'>{$needle}</span>", $line);
echo '<hr>';
}
fclose($fn);