PHP Classes

PHP File Get Word: Process the text of a file word by word

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 205 All time: 8,432 This week: 488Up
Version License PHP version Categories
fgetword 1.0.0The PHP License5PHP 5, Files and Folders, Text proces...
Description 

Author

This class can process the text of a file word by word.

It provides static functions and global functions that wrap around that can read a given file and read text words to process them individually. Currently it can:

- Get a single word from an opened file
- Search for a given word and return the number of times it was found in a file
- Return the list of words or count words found in files of a given directory
- Count the number of words in a file

Innovation Award
PHP Programming Innovation award nominee
September 2018
Number 4
Some applications need to process text word by word rather than character by character.

This class provides a solution that can process text in terms of words providing several useful functions to manipulate texts one word at a time.

Manuel Lemos
Picture of zinsou A.A.E.Moïse
  Performance   Level  
Name: zinsou A.A.E.Moïse is available for providing paid consulting. Contact zinsou A.A.E.Moïse .
Classes: 50 packages by
Country: Benin Benin
Age: 34
All time rank: 6761 in Benin Benin
Week rank: 215 Up1 in Benin Benin Equal
Innovation award
Innovation award
Nominee: 23x

Winner: 2x

Example

<?php
require_once('fgw.class.php');

echo
'<pre>';
highlight_string('
<?php
/*use a callback to filter and sort by number of occurrences ascending*/
print_r(wordsOccurrences(__DIR__,true,function($word){return strlen($word)>4?true:false;}));
?>'
);
print_r(wordsOccurrences(__DIR__,1,function($word){return strlen($word)>4?true:false;}));

highlight_string('
<?php
/*not use a callback to filter and sort alphabetically ascending*/
print_r(wordsOccurrences(__DIR__));
?>'
);
print_r(wordsOccurrences(__DIR__,-1));

highlight_string('
<?php
/*not use a callback to filter nor sort,just keep the appearance order*/
print_r(wordsOccurrences(__DIR__));
?>'
);
print_r(wordsOccurrences(__DIR__));

highlight_string('
<?php
/*count words in the current directory and in the current file using a callback to filter*/
echo countWordsInDir(__DIR__,function($word){return strlen($word)>2?true:false;}).\'<br>\';
$handle=fopen(__FILE__,\'r\');
echo countWordsInFile($handle,function($word){return strlen($word)<5?true:false;}).\'<br>\';
?>'
);
echo
countWordsInDir(__DIR__,function($word){return strlen($word)>2?true:false;}).'<br>';
$handle=fopen(__FILE__,'r');
echo
countWordsInFile($handle,function($word){return strlen($word)<5?true:false;}).'<br>';


highlight_string('
<?php
/*the two code below do the same thing yield a word from the current file resource while moving the pointer*/
rewind($handle);
foreach(yieldWordsFromFile($handle) as $word){
    echo $word.\'<br>\';
}
echo ftell($handle).\'<br>\';

rewind($handle);

while($word=fgetword($handle)){
    echo $word.\'<br>\';
}
?>'
);
rewind($handle);
foreach(
yieldWordsFromFile($handle) as $word){
    echo
$word.'<br>';
}
echo
ftell($handle).'<br>';

rewind($handle);

while(
$word=fgetword($handle)){
    echo
$word.'<br> ';
}
echo
ftell($handle);

"don't do it man";
echo
'</pre>';


?>


Details

This package extends the basic PHP files system functions with some simple but useful functions: -fgetword acts exactly as fgetc or fgets but return instead a word from the chosen file resource -yieldWordsFromFile is a generator useful to yield word from file until there is no more word left -countWordsInFile use the yieldWordsFromFile generator and increment a counter to finally return the number of words in a file -yieldWordsFromDir generator useful to yield word from directory until there is no more word left -countWordsInDir use the yieldWordsFromDir generator and increment a counter to finally return the number of words in a directory -wordsOccurrences return an array of words occurrences contained in a file or a directory sorted alphabetically or by number of occurrences descending or just by order of appearance.The returned value of this function is an array where each found file is linked to related words sorted as said above. It can be useful mainly to index file and its contents.Each word is associated to the number of occurrences. the different functions also get an optional filter argument: Its purpose is to allow custom word filtering via callback for example : you can use a spell check database or any dictionary to make the class ignore some words the design of such function is simple.It must only take one argument(the word) and must return true if the word is accepted and false if it is rejected. Indeed the package has its own definition of what is a word but this must not prevent you to specify your criteria according to the goal your are trying to achieve.That is why the filter option exists. You can of course extend the class to completely rewrite the fgetword function or just use the code as inspirational example to build something great.

  Files folder image Files (3)  
File Role Description
Accessible without login Plain text file fgetwordTest.php Example example script
Plain text file fgw.class.php Class class source
Accessible without login Plain text file readme.txt Doc. readme

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:205
This week:0
All time:8,432
This week:488Up