RE: Re[2]: [PHP] Searching for text in a file
| From: | Kelly Barrett | Date: | Mon, 10 Sep 2001 08:26:44 +0000 |
| Subject: | RE: Re[2]: [PHP] Searching for text in a file | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-66697@lists.php.net to get a copy of this message | ||
Richard,
I just put together a script using that code exactly, and it worked.
All I can say is make sure that what you are searching for is actually in
the file you are searching.
Remember that the string you are searching for needs to match the entire
contents of the line, not just a substring of the line.
Try some simple tests that you know are sure to work. i.e. Make a
virtusertable file that is just a couple of lines.
> Hello Kelly,
> Yes I noticed the variable after I sent the message
> Now I have it like this. But If I add something that is in the file it
> does not find it.
>
> $fd = fopen ("virtusertable", "r");
> $variable= "test@www.northpole.com";
> $count = 1;
> while(feof($fd) == false) {
> $line_text = fgets($fd, 2048);
> if ($variable == trim($line_text)) {
> echo "Match at line number $count";
> break;
> }
> ++$count;
> }
>
>
> Monday, September 10, 2001, 12:39:15 AM, you wrote:
>
> Kelly Barrett> Hi Richard,
> Kelly Barrett> First, I think it should be:
> Kelly Barrett> $variable = "jon@tester.com";
> Kelly Barrett> instead of:
> >> $line_text= "jon@tester.com";
>
> Kelly Barrett> as you overwrite $line_text with your fgets.
>
> Kelly Barrett> Also:
> >> if ($variable = trim($line_text)) {
> Kelly Barrett> Should be:
> Kelly Barrett> if ($variable == trim($line_text)) {
>
> Kelly Barrett> Otherwise you are doing an assignment, which will
> always be true.
>
> Kelly Barrett> Finally, your while loop should probably be:
> Kelly Barrett> while(feof($fd) == false) {
> Kelly Barrett> $line_text = fgets($fd, 2048);
>
> Kelly Barrett> Because fgets returns EOF at the end of a file,
> not necessarily false (I
> Kelly Barrett> THINK EOF currently is false, though theoretically
> EOF could change to any
> Kelly Barrett> value).
>
> Kelly Barrett> Cheers,
> Kelly Barrett> Kelly.
>
> >> -----Original Message-----
> >> From: Richard Kurth [mailto:rkurth@pacifier.com]
> >> Sent: Monday, 10 September 2001 5:12 PM
> >> To: php
> >> Subject: [PHP] Searching for text in a file
> >>
> >>
> >> I am having a problem with searching through a file for a curtain
> >> text. Like the text below jon@tester.com does not exists in the file
> >> but when I run this it gives me a Match at line number.
> >> I need to run three or four names at a time through this script
> >> to see if they are already there. But it does not seam to work.
> >> And I can not figure out way. A small sample of the file is below.
> >>
> >> $fd = fopen ("virtusertable", "r");
> >> $line_text= "jon@tester.com";
> >> $count = 1;
> >> while ($line_text = fgets($fd, 2048)) {
> >> if ($variable = trim($line_text)) {
> >> echo "Match at line number $count";
> >> break;
> >> }
> >> ++$count;
> >> }
> >>
> >>
> >> gary@www.domain.com
> >> yourns@www.yourns.com
> >> root@www.nellsouth.com
> >> owner-majordomo@www.dellwebservices.com
> >> sys@www.hispeedmail.com
> >> sys@www.register.com
> >> majordomo-owner@www.register.com
> >> vellsouth2@www.vellsouth.net
> >> majordomo-owner@www.vellsouth.com
> >> root@www.vellsouth.com
> >> majordomo@www.dellwebservices.com
> >> sys@www.cyberheadlinenews.com
> >> vellsouth@www.vellsouth.com
> >> nobody@www.register.com
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >> Best regards,
> >> Richard
> >> mailto:rkurth@pacifier.com
> >>
> >>
> >> --
> >> 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
> >>
> >>
>
>
>
>
>
> --
> Best regards,
> Richard
> mailto:rkurth@pacifier.com
>
>
> --
> 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
>
>