Php:get first word from text file
floppy
-
floppy -
floppy -
Hello,
Here is my problem.
I need to retrieve the first word written in a text file using PHP.
I can't find out how to do it.
Can someone please give me a function to use?
Thank you in advance...
Configuration: Windows 7 / Internet Explorer 8.0
Here is my problem.
I need to retrieve the first word written in a text file using PHP.
I can't find out how to do it.
Can someone please give me a function to use?
Thank you in advance...
Configuration: Windows 7 / Internet Explorer 8.0
2 answers
-
Assuming that the words are separated by spaces... (like in a normal sentence) we can do:
explode(' ', $text);
For example:$text = file_get_contents('file.txt'); $e = explode(' ', $text); $first_word = $e[0];
Here $e is an array that contains each word, for example by doing$e = explode(' ', 'this is a sentence');
We have $e[0] = 'this', 1 = 'is', 2 = 'a' and 3 ='sentence' -