Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to search for a line in an html file

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
<?php
$fout = 0;
if ((strlen($email)<2))
{
$foutmelding = &quot;<font color=white><b><li>U heeft een ongeldig emailadres ingegeven&quot;;
$fout = 1;
}

if(strlen($reactie) < 2)
{
if($fout==1)
$foutmelding .= &quot;<BR><li> U heeft geen reactie ingegeven</font></b>&quot;;
else
{
$foutmelding = &quot;<li>U heeft geen reactie ingegeven</font></b>&quot;;
$fout=1;
}
}
else
$foutmelding .=&quot;</font></b>&quot;;
if ($naam==&quot;&quot;)
$naam=&quot;Anoniem&quot;;
if($fout==1)
{
header(&quot;Location:gastenboek.php?naam=$naam&email=$email&reactie=$reactie&foutmelding=$foutmelding&quot;);
exit;
}
else
{
$bestand = &quot;reacties.html&quot;;
$bestandsindex = fopen($bestand,&quot;r+&quot;);
for($i=0;$i<count($bestandsindex);$i++)
if($bestandsindex[$i] == &quot;<!-- reacties -->&quot;)
fwrite($bestandsindex,&quot;<br>Naam: $naam <br>E-mail: $email <br>Reactie:<br>$reactie<br><hr>&quot;);
fclose($bestandsindex);
}
header(&quot;Location:reacties.html&quot;);
?>

If you take a look at the last else clause, you'll see a for clause. In this clause I use the $i variable to see if the line in that html file corresponds with the string &quot;<!-- reacties -->&quot;. If this is the case, the string in the fwrite clause has to be inserted between that line and the rest of the html file. But is doesn't work. They've already told me to use fseek, but this requires an offset and I don't know how many bites it is to that line. I know that my method works in Perl, but it doesn't seem to work in PHP. Can someone help me ?
 
Here you go:
Code:
<?php
$file = &quot;reacties.html&quot;;
$insert_loc = &quot;<!-- reacties -->&quot;;

$insert_content = &quot;<br>Naam: $naam <br>E-mail: $email <br>Reactie:<br>$reactie<br><hr>\n&quot;;

$content = @file($file);

for($i=0;$i<count($content);$i++) {
     if(ereg($insert_loc,$content[$i])) {
          $content[$i] = $content[$i].$insert_content;
     }
     $new_content .= $content[$i];
}

$fp = fopen($file,&quot;w&quot;);
fwrite($fp,$new_content);
fclose($fp);
?>
I created a file called reacties.html that initially contained:

<html>
<head>
</head>
<body>
<!-- reacties -->
</body>
</html>

I ran the script passing the appropriate parameters and the results were:

<html>
<head>
</head>
<body>
<!-- reacties -->
<br>Naam: Chad5 <br>E-mail: chorton@inlandpac.com <br>Reactie:<br>So-So Wow!<br><hr>
<br>Naam: Chad4 <br>E-mail: webmaster@inlandpac.com <br>Reactie:<br>Extreme Wow!<br><hr>
<br>Naam: Chad3 <br>E-mail: support@inlandpac.com <br>Reactie:<br>Awesome Wow!<br><hr>
<br>Naam: Chad2 <br>E-mail: info@inlandpac.com <br>Reactie:<br>Super Wow!<br><hr>
<br>Naam: Chad <br>E-mail: chorton@inlandpac.com <br>Reactie:<br>Wow!<br><hr>
</body>
</html>

Have fun!
Chad. ICQ: 54380631
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top