2014-04-17 52 views
0

我想在PHP中顯示條形圖/圖形中字符串的字符數量。我有這個功能:PHP如何打印條形圖/圖形中字符串的字符數量?

function characterCount($amount) { 
    $a = substr_count($amount, 'a'); 
    $b = substr_count($amount, 'b'); 
    return $a; 
    return $b; 
} 

echo characterCount("abbaabbaaa"); 

如何將結果打印在條形圖/圖形中?

Tnx!

+0

你將不得不穀歌 「的PHP條形圖。」我建議使用[Google Charts](https://developers.google.com/chart/interactive/docs/gallery) – user1477388

+0

使用CSS創建一個帶有顏色的Div。根據您的計算結果調整高度。數字越高,酒吧越高。 – durbnpoisn

+0

'

 
'。另外...兩個返回語句?這不會奏效... –

回答

1

你需要做這樣的

<style> 
.showbar { 
    width: 8px; 
    margin: 1px; 
    display: inline-block; 
    position: relative; 
    background-color: #aeaeae; 
    vertical-align: baseline; 
} 
</style> 

<?php 

function characterCount($amount) 
{ 
    $a = substr_count($amount, 'a'); 
    $b = substr_count($amount, 'b'); 

    return array($a,$b); 

} 

$r=characterCount('aaab'); 
echo $r[0].'<div style="height: '.$r[0].'em;" class="showbar"></div>'.$r[1].'<div style="height: '.$r[1].'em;" class="showbar"></div>'; 

輸出:

enter image description here

+0

Tnx哥們!這對我有用! – TheSnarlingBarrel

+0

歡迎你的朋友:) –

1

您的代碼已經過時了,因爲您好像不瞭解return的工作原理。

另外,已經有一個功能count_chars內置,你想要什麼。

至於在圖表中顯示,最簡單的方法是快速搜索一些圖形插件。就個人而言,我會使用HTML,一組<div>並排設置它們的height設置爲相對頻率,但實際上列出的方式太多了。去試試吧,如果你有更多的問題,那麼回到一個更具體的問題:)

現在,雖然,我希望count_chars函數讓你在正確的道路上。