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);
- 2 回答
- 0 关注
- 312 浏览
添加回答
举报
0/150
提交
取消