Page 1 of 1

how to search a string from a text file

Posted: Fri Jul 12, 2002 10:44 pm
by harsha
i have stored data in a text file "data.txt" and i want to search a email address
form that file can i do like this

$filename="data.txt";
$fptr=fopen($filename,"r");
$content=fread($fptr,filesize ($filename));
fclose($fptr);
if(strstr($content,$email))
{
echo <<< THIS
<font color="red" size="+2" face="verdana">
You have already signed the guestbook
</font>
THIS;

will this work or there is any thing to be added or deleted
plz help me :cry: :wink:

Posted: Fri Jul 12, 2002 11:27 pm
by volka
the heredoc-syntax is quite fragile.
strip the whitespace after echo <<< THIS and make sure the ending THIS is at the beginning of the line and it shall work

Posted: Sat Jul 13, 2002 1:27 am
by gnu2php
You also need to add a closing brace } at the end.

The following works, provided you set $email to something:

Code: Select all

<?php
$filename="data.txt";
$fptr=fopen($filename,"r");
$content=fread($fptr,filesize ($filename));
fclose($fptr);
if(strstr($content,$email))
&#123;
echo <<<THIS
<font color="red" size="+2" face="verdana">
You have already signed the guestbook
</font>
THIS;
&#125;
?>
Note that if you copy-and-paste this code, you'll need to remove the extra space after:

Code: Select all

echo <<<THIS