5.6 Working with Delimited Records


You want to read values separated by a record separator that you define. This can be useful when parsing large complex files.

Technique

Use the explode() function to divide your string into an array from your specified record separator:

 <?php $line = explode (':', $line); ?> 

Or, if you need more power, use the preg_split() function to tear apart your string:

 <?php // split HTML by tags $text = preg_split ('/<[^>]+>/', $html); ?> 

Comments

Separating records by text can be extremely useful if you have large text files and the normal line break just won't cut it as a separator. This is especially appropriate when handling binary file uploads with enormous sizes.

Use the explode() function whenever possible because it gives a great speed increase over the preg_split() function. However, if you need more power, the preg_split() function is about as powerful as you can get.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net