home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomePHP CookbookSearch this book

18.13. Processing Variable Length Text Fields

18.13.2. Solution

Read in each line and then split the fields based on their delimiter:

$delim = '|';

$fh = fopen('books.txt','r') or die("can't open: $php_errormsg");
while (! feof($fh)) {
    $s = rtrim(fgets($fh,1024));
    $fields = explode($delim,$s);
    // ... do something with the data ... 
}
fclose($fh) or die("can't close: $php_errormsg");

18.13.4. See Also

Section 1.12 discusses ways to break apart strings into pieces; Section 1.10 and Section 1.11 cover parsing comma-separated and fixed-width data; documentation on explode( ) at http://www.php.net/explode and rtrim( ) at http://www.php.net/rtrim.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.