Posted by & filed under General, Tutorials.

I recently had a go at creating a webpage that used HTML input and PHP to count the number of different characters in a string. I then went on to create a page that split a string or word by picking out every nth character, determined by the user. These came into very handy use when doing my codes and codebreaking module homework, as that involves a lot of repetitive counting of letters.

Here is the code for counting the number of characters, and character frequency in a word or string, and then displaying results in a table. The HTML form posts to the same page. It uses the count_chars PHP function, as well as the chr() function, to determine which characters appear. This set up currently can only recognise characters A-Z, but by using different ASCII values, it can count any.

[php]</pre>
<div id="wrapper">
<pre><div id="wrapper">
<p>Count frequency of characters in a string – <a href="split.php">click here for Splitting a string</a></p>
<?php

$word=$_REQUEST[word];
$upperword= strtoupper($word); //Make word uppercase
echo "<h2>Word:</h2> $upperword <br>";
$array=count_chars($upperword, 1);
//print_r($array);
echo "<table><th>Character</th>
<th>Frequency</th>";
for ($counter=65;$counter<91;$counter++) { //ASCII keys for capitals is A=65, Z=90
{
echo "<tr><td>";
echo chr($counter);
echo "</td><td>$array[$counter] <br></td></tr>";
}
}
echo "</table><br>";
?>

<form action="characters.php" method="POST">
<input type="text" name="word" class="first" placeholder="word/string"/>
<input type="submit" value="Submit" id="submit"/>
</form>

</div><!–end wrapper–></pre>
</div>
<pre>
[/php]

In codebreaking, you often need to use every other character, or every nth character, so I created the split program for that purpose.[php]</pre>
<p>Count the characters in a string, and use the drop-down box to output every nth character (default is 1, so normal word output)</p>
<?php
$word=$_POST[word];
$everycharacter=$_POST[every];
$startingpoint=$_POST[starter];
if ($startingpoint==FALSE)
{$startingpoint=1;}
$upperword= strtoupper($word); //Make word uppercase
$wordarray=str_split($upperword);//Uppperword into an array called wordarray
$charactercount=strlen($upperword); //character count
array_unshift($wordarray,"blank");//Makes the first value in the array [0]=blank

$newstringarray=array(" ");//make new array for the result, with [0] set as a blank space
$wordarraybackup=array();

foreach ($wordarray as $value) //backup
{
$wordarraybackup[]=$value;
}

echo "<br><br><h2>Result:</h2>";
echo $wordarray[$startingpoint];
$newstringarray[]=$wordarray[$startingpoint];
for ($i=$startingpoint;$i>0;$i–)
{
array_shift($wordarray);//deletes first few values in the array, to get to the starting point
}

for($counter=1;$counter<=$charactercount;$counter++)
{
if ($counter % $everycharacter==0)
{
echo $wordarray[$counter]; //this prints out every nth character – +1 as we start counting from the second character
$newstringarray[]=$wordarray[$counter]; //this assigns that character to an array
}
}

array_shift($newstringarray);
$newword=implode("",$newstringarray); //this puts all the new characters from the array into a string
echo "<br><h2>Orig Char Count:</h2> ". $charactercount ."<br>";
$charactercount=strlen($newword); //character count
echo "<h2>New Char Count:</h2> ". $charactercount;

?>
<br><br>
<form action="split.php" method="POST">
<input type="text" name="word" class="first" placeholder="word/string" />
<input type="submit" value="Submit" id="submit"/><br>
<div class="droplabel"><p>Pick out Nth Character:</p>
<select name="every" class="dropdown">
<?php
for ($i=1;$i<=10;$i++)
{
echo "<option value=’$i’>$i</option>";
}
?>
</select>
</div><!–end droplabel–>
<div class="droplabel"><p>Starting character:</p>
<select name="starter" class="dropdown">
<?php
for ($i=1;$i<=10;$i++)
{
echo "<option value=’$i’>$i</option>";
}
?>
</select>
</div><!–end droplabel–>
</form>

<?php
echo "<a href=’characters.php?word=$newword’>Click here for character frequency</a>";
?>[/php]

Leave a Reply

  • (will not be published)