Jump to content

Getting a histogram to display number of occurences of a string


Crisam

Recommended Posts

Hello I am trying to recreate a histogram I made in Java in PHP. The problem is as follows:

 

Make a function that counts the number of string occurences to produce a histogram using PHP.

 

I got a good idea that the [A-Z,a-z and any other characters such as !,. etc] are needed to be included as well. I just would like a HTML output

do you mean

<?php
$str = "Now's the time for all good men to come to the aid of the party!";
$counts = array_count_values(str_split(strtoupper($str)));
ksort($counts);
#unset($counts[' ']);
echo '<pre>';
foreach ($counts as $k=>$n)
   printf ('%s %3d %s<br>', $k, $n, str_repeat('*', $n));
echo '</pre>';
?>

 

or do you mean something a little more sophisticated?

$str = $data;
$data= array (



);

// I want to be able to write a script that can be reused and display a histogram using a bars or any //shape



$max = max($data);
echo '';
foreach ($data as $k=>$v)
{
// display outputs in a table in HTML here
}
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.