I'm trying to count the vowels in a lengthy text, provided by a .txt file. I can successfully open the file and echo it out into the browser. What I can't seem to do is get my script to do the actual vowel counting and I'm not entirely sure why. I'm supposed to output the vowel count to a file that doesn't previously exist, but is referred to as "file_output1.txt". I'm not sure if this is what is causing the issue, if I'm not properly accessing the file to enable the count to occur, or if I made a syntax error my eyes just can't seem to catch right now.

This is what I've done so far, but I'm not getting the count to fill. There are hundreds of vowels in the text file and it keeps spitting out: "There are (0) vowels". I have counted letters in a string before, but I am having trouble doing it with the file. Any advice?

<?php
#openfile

$file = "Assignment2inputfile.txt" ;

$document = fopen($file,r);

echo fread($document,filesize("Assignment2inputfile.txt"));

?>
<html>
<br><br>
</html>
<?php
#vowelcount

$vowels = array("a", "e", "i", "o", "u"); 
$length = strlen($_POST["file_output1.txt"]);
$count = 0; 

for ($i =0; $i = $length; $i++)
{
    if (array_search($_POST["file_output1.txt"][$i], $vowels))
    {
        $count++; 
    }
}
echo 'There are (' . $count . ') vowels ' . $_POST["file_output1.txt"] .'.';  ;
fclose($document);  
?>

This is how I counted letters before, but this time it is not a short string input. How can I do this, for the vowels, but with a file? Should it work about the same?

<?php 

  $string = "UniversityofNorthTexasDepartmentofInformationScience";
  $tcount = substr_count($string,t);
  $Tcount = substr_count($string,T); 

  print "Total amount:" ;
  print $tcount + $Tcount ;
?>

Good link rp +1. You may benefit from using a strtolower on the string so you only have to check aeiou instead of also having to check AEIOU. There are many many ways to do this. Using count_char() and using ascii strings is my favourite as it doesn't use array functions unless you count count_char itself, of course. A further complication may be accented characters.

commented: The craic was mighty! +15
$lc = count_chars(strtolower($str));
$total = $lc[97] + $lc[101] + $lc[105]+ $lc[111]+ $lc[117] ;

A good reason for this is that you don't have to isset for each element as each ascii key is set automatically, regardless of whether the character exists in the string.

commented: Qapla’ +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.