Jump to content

Getting a histogram to display number of occurences of a string


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

Edited by Crisam

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
}
?>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.