Page 1 of 1

Pattern recognition/finding how many a's in a string/array

Posted: Sun Apr 30, 2006 9:32 pm
by frozendice
I'm writing a script and the part I have so far takes a string from a REQUEST and puts it into an array becaues I'm using a version below PHP 5, and now I'm looking for a way to Search through that array and find how many a's, b's, c's and so on are in the message, be able to output the largest, letters that appear together frequently, etc. I'm making this to demo some basic cryptography lessons about statisticly how often letters show, patterns, simple substitution ciphers.

Are there any PHP methods already impelmented that can help me do this? Tell me how many somethings are in an array or string?

Thank you so much!

Posted: Sun Apr 30, 2006 9:41 pm
by feyd
Can you post your code?

Posted: Mon May 01, 2006 12:56 am
by frozendice
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


heh, don't blame me when you go blind looking at awefull code.  I only use PHP when I have a project to make and that's with a heavy use on PHP.net's function search.

Code: Select all

<?php

$rrr = $_REQUEST['code'];

// remember to not output $rrr in the textbox without striping for attacks!

echo '
<form action=" ' . $_SERVER["PHP_SELF"] . ' " method="request">
   Code:  <input type="textfield" name="code" value= "' . $rrr . '" />
   <input type="submit" name="submit" value="Process" />
</form>
';




// I don't have PHP 5 so I have to use this work arround.
   function str_split($string,$split_length=1){
       $count = strlen($string); 
       if($split_length < 1){
           return false; 
       } elseif($split_length > $count){
           return array($string);
       } else {
           $num = (int)ceil($count/$split_length); 
           $ret = array(); 
           for($i=0;$i<$num;$i++){ 
               $ret[] = substr($string,$i*$split_length,$split_length); 
           } 
           return $ret;
       }     
   } 


$arr1 = str_split($rrr);
$inlen = strlen($rrr);

// begin alaysis

        $arr2 = array(); // this block of code shoudl compare the first with the second then third etc, each item finding a match it adds 1 to the second array.  
        for($p=0;$p<$inlen;$p++){

           for($i=0;$i<$inlen;$i++){ 

               if($arr1[i] == $arr1[p])
		$arr2[p] = $arra2[p] + 1;
            } 
	}

//output untouched array
print_r($arr1);

echo '<br><br>';

//output counted
print_r($arr2);
?>
Now that I think about it I would get an array the same size as the message with repeat values. Arrrgh. How can I make this work?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon May 01, 2006 3:28 am
by someberry

Code: Select all

<?php

//--- str_split ------------------------------
function str_split($string,$split_length=1){
   $count = strlen($string);
   if($split_length < 1){
      return false;
   } elseif($split_length > $count){
      return array($string);
   } else {
      $num = (int)ceil($count/$split_length);
      $ret = array();
      for($i=0;$i<$num;$i++){
         $ret[] = substr($string,$i*$split_length,$split_length);
      }
      return $ret;
   }
}

//--- count_letters --------------------------
function count_letters($theArray)
{
   foreach($theArray as $key => $val)
   {
      $arr4[$val] = substr_count($_GET['code'], $val);
   }

   return $arr4;
}

echo '
<form action=" ' . $_SERVER["PHP_SELF"] . ' " method="get">
Code: <input type="textfield" name="code" value= "' . $rrr . '" />
<input type="submit" name="submit" value="Process" />
</form>
';


if(!empty($_GET['code']))
{
   $arr = str_split($_GET['code'],1);
   print_r(count_letters(array_unique($arr)));
}
?>
Inputs:

Code: Select all

hello

bobby

A long string of characters
Outputs:

Code: Select all

Array ( [h] => 1 [e] => 1 [l] => 2 [o] => 1 )

Array ( [b] => 3 [o] => 1 [y] => 1 )

Array ( [A] => 1 [ ] => 4 [l] => 1 [o] => 2 [n] => 2 [g] => 2 [s] => 2 [t] => 2 [r] => 3 [i] => 1 [f] => 1 [c] => 2 [h] => 1 [a] => 2 [e] => 1 )
As you can see, it is case sensitive. If you don't want it case sensitive then you can just use strtoupper() or strtolower().

It also counts the whitespace, if you dont want it to, just use trim() to get rid of that.

Posted: Mon May 01, 2006 4:50 am
by s.dot
someberry wrote:As you can see, it is case sensitive. If you don't want it case sensitive then you can just use strtoupper() or strtolower().
Or, strcasecmp()

And I must say, that is a dandy str_split function ;)

Posted: Mon May 01, 2006 7:39 am
by someberry
scottayy wrote:
someberry wrote:As you can see, it is case sensitive. If you don't want it case sensitive then you can just use strtoupper() or strtolower().
Or, strcasecmp()

And I must say, that is a dandy str_split function ;)
Yea, or that function.

The str_split() isn't too bad, there is no need for the $split_length=1 in the parameters, it would be better to pass it in through the function call - it is limiting the function somewhat. Also, I would have used the round() function over the ceil() function. But heh, thats just me.