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
5045 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
Peter Kelly

Website Development Tutorials Written by Peter Kelly (http://www.peter-kelly.me).
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top