Forum Moderators: coopster

Message Too Old, No Replies

A newbie question.

         

jambojim

6:04 pm on Sep 17, 2008 (gmt 0)

10+ Year Member


I am currently editing a PHP script I located on the internet. I have created a multi-line file that inputs to the screen. I am using the following command:

$output=readfile($file);

The command works just fine. My problem is I don't want all of the information within the file to display. Two of the lines of information are a phone number (phone) and an email address (email). How do I go about suppressing the phone and email information so it remains in the file, but does not display on the screen?

Thank you for any assistance you may provide.

jambojim

eelixduppy

7:01 pm on Sep 17, 2008 (gmt 0)



Do you want everything in the file to be output to the browser? Maybe you'd be better of with file_get_contents [us2.php.net].

Welcome to WebmasterWorld, btw :)

Anyango

7:12 pm on Sep 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey, what you can do is you can use fgets to get every line one by one from your file and put a check, if any line contains the information which you dont want to show u dont echo it

<?php
$file = fopen("yourfile.txt", "r") or exit("Unable to open file!");

while(!feof($file))
{
$thisLine=fgets($file);
//check $thisLine variable here, either with
//regular expression or strpos to see if this
//line contains email or phone number
// and if not, set $canShowThisLine to true;
if($canShowThisLine)
{
echo $thisLine;
}
}
fclose($file);
?>

Reference: [w3schools.com...]

Anyango

7:17 pm on Sep 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or check this

[webmasterworld.com...]

Check Last Message from jshpro2, you just need to invert his "if" so that if your line matches the regex you dont echo and if it doesnt, you echo.

jambojim

7:41 pm on Sep 17, 2008 (gmt 0)

10+ Year Member



Thank you to all who have offered solutions to this problem, thus far. I thought that by posting the code in question it would help to answer my question. There is an HTML page that this code works with. Please let me know if I should post that info, too.

Now, I am TRULY a newbie at this, so I must admit the solutions provided don't make a lot of sense to me - yet! Can anyone inform me just how I may incorporate their solution in the existing code?

Thanks, again, folks.

jambojim

<?php

//form code for user information input
print "<div align=\"center\">";
print "<p><font size=\"+1\">Database Workshop</font></p>";
print "<table width=\"80%\" border=\"0\">";
print " <tr><td><div align=\"center\">To register for this workshop, please enter your information and click Submit.
</div></td></tr>";
print "<tr><td bgcolor=\"#CCCCCC\"><form name=\"form1\" method=\"post\" action=\"workshop.php\">";
print "<table width=\"100%\" border=\"0\">";
print "<tr><td width=\"44%\"><div align=\"right\">Name:</div></td>";
print "<td width=\"56\"><input type=\"text\" name=\"name\"></td></tr>";
print "<tr><td><p align=\"right\">Email Address:</p></td>";
print "<td><input type=\"text\" name=\"email\"></td></tr>";
print "<tr><td><p align=\"right\">Phone Number:</p></td>";
print "<td><input type=\"text\" name=\"phone\"></td></tr>";
print "<tr><td><div align=\"right\">Comments:</div></td>";
print "<td><textarea name=\"comments\" cols=\"30\" rows=\"4\"></textarea></td></tr>";
print "<tr><td colspan=\"2\"><div align=\"center\">";
print "<input type=\"submit\" name=\"Submit\" value=\"Submit\">";
print "<input type=\"reset\" name=\"reset\" value=\"Reset\"></div></td></tr></table></form></td></tr></table><br></font></div>";

//reads counter from "count.dat" file - will need to be preset to number of students in workshop
$counter_file="count.dat";
if(!($fp=fopen($counter_file,"r"))) die ("Cannot open $counter_file.");
$counter=(int)fread($fp,20);
fclose($fp);

//emails information to myname!@xx.edu
$to="myname@xx.edu";
$subject="New Database Workshop Registration";
$body="$comments";
mail($to,$subject,$body);

//rolls counter down 1 for display purposes
$count=$counter-1;
//gets current date ( Month Day Year)
$date=gmdate("M d Y");
//assign data file "registrants.txt"
$file="registrants.txt";

//if the remaining student counter is above 0, writes registrant info to file formatted for html
if($counter!=0)
{
$fp=fopen($file,"a+");
fwrite($fp,"<div align=\"center\"><table width=\"70%\"><tr><td><hr noshade width=\"60%\"><h3>Registrant #$counter</h3><br><b>Date: </b>$date \n<br><b>Name: </b>$name \n<br> Phone: $phone \n<br> <b>Email: </b><a href=\"mailto:$email\">$email</a> \n<br><br><b>Comments:</b>$comments</font>\n\n<br><hr noshade width=\"60%\"></td></tr></table></div>");
//if there are more than 2 positions left, thanks registrant for registering
if($counter>2)
print "<div align=\"center\"><h2>Thank you for registering, there are $count positions left.</h2></div>";

//if there is only one position left, thanks registrant for registering
elseif($counter==2)
print "<div align=\"center\"><h2>Thank you for registering, there is $count position left.</h2></div>";

//if there are no positions left, thanks registrant for registering
elseif($counter==1)
print "<div align=\"center\"><h2>Thank you for registering, there are no available positions left.</h2></div>";
fclose($fp);
}
//if there are no positions left when submit is clicked, user is notified
else
print "<div align=\"center\"><h2>This workshop is completely full.<br>Please register for another workshop in the future.</h2></div>";

//rolls counter down 1, and writes the new value to the "count.dat" file
if($counter>0)
$counter--;
else
$counter=0;
$fp=fopen($counter_file,"w");
fwrite($fp,$counter);
fclose($fp);

//outputs the contents of the file to the screen
$output=readfile($file);

?>