suttercain Posted March 17, 2007 Share Posted March 17, 2007 Hi Everyone, I am running this code: <?php $lines = file('http://localhost/PHP%20Tutorials/By%20Example/examples/chapter10_examples/datebook.bck'); // Loop through our array, show HTML source as HTML source; and line numbers too. foreach ($lines as $line_num => $line) { echo "<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n"; } ?> It is outputting the following: 0 : Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300 1 : Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500 2 : Igor Chevsky:385-375-8395:3567 Populus Place, Caldwell, NJ 23875:6/18/68:23400 3 : Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700 4 : Jennifer Cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900 5 : Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100 6 : Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92086:7/25/53:85100 7 : Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200 8 : Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200 9 : Fred Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/12/23:780900 10 : Fred Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/12/23:780900 11 : Lori Gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200 12 : Paco Gutierrez:835-365-1284:454 Easy Street, Decatur, IL 75732:2/28/53:123500 13 : Ephram Hardy:293-259-5395:235 CarltonLane, Joliet, IL 73858:8/12/20:56700 14 : James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000 15 : Barbara Kertz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/1/46:268500 16 : Lesley Kirstin:408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600 17 : William Kopf:846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500 18 : Sir Lancelot:837-835-8257:474 Camelot Boulevard, Bath, WY 28356:5/13/69:24500 19 : Jesse Neal:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000 20 : Zippy Pinhead:834-823-8319:2356 Bizarro Ave., Farmount, IL 84357:1/1/67:89500 21 : Arthur Putie:923-835-8745:23 Wimp Lane, Kensington, DL 38758:8/31/69:126000 22 : Popeye Sailor:156-454-3322:945 Bluto Street, Anywhere, USA 29358:3/19/35:22350 23 : Jose Santiago:385-898-8357:38 Fife Way, Abilene, TX 39673:1/5/58:95600 24 : Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200 25 : Yukio Takeshida:387-827-1095:13 Uno Lane, Ashville, NC 23556:7/1/29:57000 26 : Vinh Tranh:438-910-7449:8235 Maple Street, Wilmington, VM 29085:9/23/63:68900 I am trying to print only certain perimeters. Like let's say I want to only output only the November and December lines. Is there an easy way to do this? Thanks. Link to comment https://forums.phpfreaks.com/topic/43084-printing-certain-things-from-a-file-not-the-entire-thing/ Share on other sites More sharing options...
ted_chou12 Posted March 17, 2007 Share Posted March 17, 2007 First I assume that your file looks like: 0 : Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300 1 : Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500 2 : Igor Chevsky:385-375-8395:3567 Populus Place, Caldwell, NJ 23875:6/18/68:23400 3 : Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700 4 : Jennifer Cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900 5 : Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100 6 : Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92086:7/25/53:85100 7 : Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200 8 : Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200 9 : Fred Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/12/23:780900 10 : Fred Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/12/23:780900 11 : Lori Gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200 12 : Paco Gutierrez:835-365-1284:454 Easy Street, Decatur, IL 75732:2/28/53:123500 13 : Ephram Hardy:293-259-5395:235 CarltonLane, Joliet, IL 73858:8/12/20:56700 14 : James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000 15 : Barbara Kertz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/1/46:268500 16 : Lesley Kirstin:408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600 17 : William Kopf:846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500 18 : Sir Lancelot:837-835-8257:474 Camelot Boulevard, Bath, WY 28356:5/13/69:24500 19 : Jesse Neal:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000 20 : Zippy Pinhead:834-823-8319:2356 Bizarro Ave., Farmount, IL 84357:1/1/67:89500 21 : Arthur Putie:923-835-8745:23 Wimp Lane, Kensington, DL 38758:8/31/69:126000 22 : Popeye Sailor:156-454-3322:945 Bluto Street, Anywhere, USA 29358:3/19/35:22350 23 : Jose Santiago:385-898-8357:38 Fife Way, Abilene, TX 39673:1/5/58:95600 24 : Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200 25 : Yukio Takeshida:387-827-1095:13 Uno Lane, Ashville, NC 23556:7/1/29:57000 26 : Vinh Tranh:438-910-7449:8235 Maple Street, Wilmington, VM 29085:9/23/63:68900 but your file extension "bck" is very weird... code should be: $data = "pathtofile"; foreach ($data as $line) { $info = explode(" ", $line); $max = count($info); $date = $info[$max]; $date1 = strtotime($date); $date2 = date('m', $date1); if ($date2 == 11 or $date == 12) {$array[] = $info[0];}} foreach ($array as $line1) { echo $data[$line1]; Ted Link to comment https://forums.phpfreaks.com/topic/43084-printing-certain-things-from-a-file-not-the-entire-thing/#findComment-209486 Share on other sites More sharing options...
kenrbnsn Posted March 17, 2007 Share Posted March 17, 2007 The strtotime() function probably won't work here since it looks like the OP has years earlier than 1970 in the file. BTW, there's nothing wrong with a file extension of ".bck". I assumed that it's a backup file of some sort. Ken Link to comment https://forums.phpfreaks.com/topic/43084-printing-certain-things-from-a-file-not-the-entire-thing/#findComment-209495 Share on other sites More sharing options...
Barand Posted March 17, 2007 Share Posted March 17, 2007 It won't be too happy with a string like "94087:5/19/66:34200" either. Also the index would be $max-1 try <?php $lines = file('http://localhost/PHP%20Tutorials/By%20Example/examples/chapter10_examples/datebook.bck'); foreach ($lines as $line_num => $line) { if (strpos($line, ':11/') || strpos($line, ':12/')) echo "<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/43084-printing-certain-things-from-a-file-not-the-entire-thing/#findComment-209558 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.