如何在php中更改文件的输出颜色

问题描述 投票:-3回答:2

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)
php
2个回答
0
投票

您无法使用PHP更改文本颜色。您必须在HTML文档中使用CSS更改文本颜色。


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);
© www.soinside.com 2019 - 2024. All rights reserved.