Re: Matching strings in a flat/text file?
| From: | Pavel Jartsev | Date: | Tue, 23 Oct 2001 07:21:40 +0000 |
| Subject: | Re: Matching strings in a flat/text file? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-71907@lists.php.net to get a copy of this message | ||
Nick Richardson wrote:
>
> ...
>
> Example: I want to pass a string using the URL (blah.php?car=civic_si) then
> search for ##civic_si in the text file, and print everything until it finds
> ##end. So the text file will look like this:
>
> ##civic_si
> This is my content, blah blah
> ##end
>
> ##nissan
> This is more content... yadda yadda
> ##end
>
> ...
>
Try this little code snippet:
<?
$filename = 'cars.data';
$car = $HTTP_GET_VARS['car'];
if ($car != '') {
$file = file($filename);
if (is_array($file)) {
$read = FALSE;
foreach ($file as $f) {
if (!$read && ereg('^##'.$car, $f))
$read = TRUE;
elseif ($read && ereg('^##end', $f)) {
$read = FALSE;
break;
} elseif ($read)
$content .= $f;
}
}
}
echo "content=$content<br>";
?>
Hope this helps. :)
--
Pavel a.k.a. Papi