RE: [PHP] searching in file
| From: | Hans H. Anderson | Date: | Mon, 28 Aug 2000 16:19:59 +0000 |
| Subject: | RE: [PHP] searching in file | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-13972@lists.php.net to get a copy of this message | ||
Adam,
strpos() has an optional third argument that allows you to start n bytes
into a file, so you could do something like this (untested):
<?php
$haystack = " homer went home to the homeboy's house";
$needle = "home";
$found = -1;
$start=0;
do {
$l=strpos($haystack,$needle,$start);
$found++;
$start=$l+1;
} while ($l);
print "I found $found occurences of '$needle' in '$haystack'\n";
?>
---
The only problem I can see with that is if it matches starting at the
first character, which would be character position 0, short circuiting the
loop. Notice the space to start the $haystack variable.
Hans
On Mon, 28 Aug 2000, Adam Charnock wrote:
> Is there a way to find the number of occurrences in the file (in
> PHP3). I've seen a function in PHP 4 to do it, but not in 3.
>
> Thanks :-) Adam
>
> -----Original Message----- From: Hans H. Anderson
> [mailto:hans@hansanderson.com] Sent: 28 August 2000 17:22 To:
> Worawisut Pinyoyang Cc: php-general@lists.php.net Subject: Re: [PHP]
> searching in file
>
>
> See strstr() and strpos() in the manual.
>
> Hans Anderson
> me@hansanderson.com
> http://www.hansanderson.com/
> http://www.goofiness.com/
>
>
>
> On Mon, 28 Aug 2000, Worawisut Pinyoyang wrote:
>
> >
> > If i 'd like to search the word "Nut" in text files (I have many text
> > files). Which function i can do this job.
> >
> > Thank you very much
> >
> > W.Pinyoyang
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> > For additional commands, e-mail: php-general-help@lists.php.net
> > To contact the list administrators, e-mail: php-list-admin@lists.php.net
> >
> >
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>
>
>
>
Hans Anderson ~ me@hansanderson.com
PO Box 426, Missoula, MT 59806 | PHP, Search Engines, SQL
(800)397-9116 / (406)549-6524 | Databases, Flash Multimedia,
http://www.hansanderson.com | Killer Audio Production,
http://www.goofiness.com | Minty Fresh Breath.
UW PICO(tm) 3.5 File: x