The explode function with file handling example in php programming language. Will read the file. echo out to the user for reading the data of the file by file handling in php programming language.
Code for explode function with file handling example(Screenshot1)
<?php $filename = 'names.txt'; $handle = fopen($filename,'r'); $datain = fread($handle, filesize($filename)); $names_array = explode(',', $datain); foreach($names_array as $name) { echo $name. '<br>'; } ?>
Output (Screenshot2,)
Screenshot2 |
In this example we are going to learn to read data from a file but obviously when u are reading data from the file. Its not always line by line basis. You may find commas separating your values and in this case we got few names here commas separated each value. Now how we deal with reading this in. Php file handling reading into the file.
$handle fopen($filename, 'r');
Above code will create a handle to open and reading the file r stands for reading the file. We can also use file function which i had shown in example of file handling reading to the file. or we can also write $handle fopen('names.txt','r');. I have taken names.txt in variale $filename for futhur use.
$datain = fread($handle, filesize($filename));
Above code will inilize the filesize will tell php file contain that much data instead of writing filesize we can also write the character we want from the file. like fread($handle,100); that's means it will contain 100 characters from the file.
$names_array = explode(',', $datain);
It will explode all commas from the file. Will remove commas from the file array of the name.
foreach($names_array as $name) {
echo $name. '<br>';
}
It will get array will write names one by one.
No comments:
Post a Comment
Thanks For Comment Will get you Soon..