2017-05-01 26 views
0

我不知道如何正確創建此代碼。如何在txt文件中用bash顏色保存彩色文本?

我有這樣的代碼:

$hasil = "\e[1;42mGreen Text"; 
 
$fp = fopen('data.txt', 'w'); 
 
fwrite($fp, $hasil . "\r\n"); 
 
fclose($fp);

我想彩色文本存儲在data.txt中。

如果這樣,它不會變成彩色文本,但代碼會保存爲文本。

+0

你想在文本文件中存儲輸出什麼? –

+0

嗯,你不能。文本文件不包含任何格式信息。如果你想要的顏色,使用不同的格式(.doc,.odf,html ....) –

+0

好的,如果格式是html,那麼我需要什麼代碼? – UmRchan

回答

0

$hasil = "<a style='color:green'>Text</a>"; 
 
$fp = fopen('data.html', 'w'); 
 
fwrite($fp, $hasil . "\r\n"); 
 
fclose($fp);

在HTML文件中保存數據後打開它在瀏覽器中

或檢查另一個例子

$output = convertBash('[1;42m Text'); 
echo $output; 
// 
// Converts Bashoutput to colored HTML 
// 
function convertBash($code) { 
    $dictionary = array(
     '[1;30m' => '<span style="color:black">', 
     '[1;31m' => '<span style="color:red">', 
     '[1;42m' => '<span style="color:green">', 
     '[1;33m' => '<span style="color:yellow">', 
     '[1;34m' => '<span style="color:blue">', 
     '[1;35m' => '<span style="color:purple">', 
     '[1;36m' => '<span style="color:cyan">', 
     '[1;37m' => '<span style="color:white">', 
     '[m' => '</span>' 
    ); 
    $htmlString = str_replace(array_keys($dictionary), $dictionary, $code); 
    return $htmlString; 
} 
+0

是否不能使用bash顏色?因爲它只是我的代碼的一個例子。 – UmRchan

+0

答案已更新。 –

+0

非常感謝。 – UmRchan