Friday 28 March 2014

Explode function with file handling example

php explode implode function
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)

explode function file handling php
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,)

php explode function
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..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates