Page 1 of 1

Get and split in php text from text-file

Posted: 21 Mar 2017, 17:01
by JanMolendijk
Dear Admin i`m back again with something new...
How can i show into a php document text from an text-file ?

i have this code into the text file
name: poll_result
My text is:

Code: Select all

184||2

Factly i would like to see only the score 184 or the 2
but i`m also happy with both results (whole)

Get and split in php text from text-file

Posted: 22 Mar 2017, 07:07
by Admin
Hello,
You can use file_get_contents() to get a string with file data.
Then, with explode() you can split that string by the separator you want, and store the resulted items into an array.
- Here is a code example:

Code: Select all

$file ='path_to/filename'; //path to the text file
$fdata = file_get_contents($file); //get file data
$fdata = explode('||', $fdata); //split text data by '||', results into an array

//show data from 1st and 2nd item
echo $fdata[0];
echo '<br>'. $fdata[1];