为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
三国纷争 2021-05-03 14:35:36
Test.txt文件包含一些文本。如何从PHP文件中更改文本的输出颜色。Test.txt fileThe 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)
查看完整描述

2 回答

?
慕田峪9158850

TA贡献1794条经验 获得超7个赞

你可以做到


$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);


查看完整回答
反对 回复 2021-05-21
?
哔哔one

TA贡献1854条经验 获得超8个赞

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


查看完整回答
反对 回复 2021-05-21
  • 2 回答
  • 0 关注
  • 312 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信