Get and split in php text from text-file

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
JanMolendijk
Posts: 282
Location: Holland Rotterdam

Get and split in php text from text-file

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)

Admin Posts: 805
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];

Similar Topics