I have a text file, and I want to pull of the number of words that start with a specific letter of the alphabet. The text file has a total of something like 171,896 words that range from A-Z. Here is what I have so far as for coding. Any help would be appreciated…
[php] <?php
//reads the text file
$textfile = “wordsData.txt”;
$file = fopen("$textfile", ‘r’) or exit (“File Does not Exist!”);
// read into string
$str = file_get_contents($textfile);
// count words
//$numWords = str_word_count($str);
//echo "This file have ". $numWords . " words";
// count characters withour spaces
$str2 = ereg_replace('[[:space:]]+', '', $str);
$numChar = strlen($str2);
echo "This file have ". $numChar . " character(s) without spaces";
$string = "ab";
$var = "a";
if ($count = substr_count($string, $var)) {
echo $count. " matches found";
} else {
echo "match NOT found";
}
//$matches = null;
//preg_match('?',$textfile);
// $Data = fread($file, filesize($textfile));
fclose($file);
//echo $Data;
//echo $matches;
?> [/php]