Publishing System Settings Logout Login Register
How to explode information out of a string or a file
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on November 26th, 2008
2257 views
PHP Coding
PHP Explode function allows you to extract just certain information from a string/file ect

<? 
$choc  = "chocolate1 chocolate2 chocolate3 chocolate4 chocolate5";
$piece = explode(" ", $choc);  // Explode on the space on the $choc variable
echo $piece[0]; // chocolate1
echo $piece[1]; // chocolate2
?>


That is just to do a line by line thing but if you need to do that to a whole file e.g line by line then you need to repeat this

file.txt
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15
16 17 18 19 20 21


<? 
$file = file("file.txt");  // Open the file.txt
foreach ($file as $line) {  // Now open it and read it line by line
$chat = explode(" ",$line);  // Explode on the space on each line from the file
echo "$chat[0] - $chat[5]<br>"; // Now show the first word and the fifth word on the line
}
?>


that echos out

1 - 6
10 - 15
16 - 21


Thanks for reading and all my other tutorials are on pixel2life.com and check out my site www.pk-scripts.co.uk for others :P
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
Peterk92
www.pk-tuts.co.uk

Website Development Tutorials Written for PK-Tuts.co.uk
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Get Started Credit Corner Get Started Credit Corner