Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Reading word by word?

Tags:

file

php

parsing

I am trying to read a file one word at a time. So far I have been able to use fgets() to read line by line or up to a certain amount of bytes, but that is not what I am looking for. I want one word at a time. up to the next white space, \n, or EOF.

Does anyone know how to do this in php. In c++ I just use the 'cin >> var' command.

like image 462
Alex O'rourke Avatar asked Jun 20 '13 06:06

Alex O'rourke


1 Answers

you can do this by

$filecontents = file_get_contents('words.txt');

$words = preg_split('/[\s]+/', $filecontents, -1, PREG_SPLIT_NO_EMPTY);

print_r($words);

this will give you array of words

like image 66
NullPoiиteя Avatar answered Sep 25 '22 10:09

NullPoiиteя