how to search a string from a text file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
harsha
Forum Contributor
Posts: 103
Joined: Thu Jul 11, 2002 1:35 am
Location: Bengaluru (Bangalore) > Karnataka > India

how to search a string from a text file

Post 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:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post 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
Post Reply