Page 1 of 2
Reading From Files
Posted: Sun Aug 18, 2002 7:26 pm
by Silver_Eclipse
I am trying to read information from an HTML file into my php script and would like to know if it is possible to pinpoint certain information inside the file. I have only been able to specify the length in bytes but cannot figure out how to pinpoint exact information. Could someone please tell me if this is possible and if so how?
Posted: Sun Aug 18, 2002 7:32 pm
by hob_goblin
if its on its own line, and you know that line, you can use file()
say file.txt contains
Code: Select all
<html>
this is some code
<b>this is a tag</b>
this is some more code
</html>
i could then do this script to pull out 'this is a tag'
Code: Select all
$filename = "file.txt";
$fd = fopen ($filename, "r");
$contents = fread ($fd, filesize ($filename));
fclose ($fd);
ereg("<b>(.*)</b>", $contents, $match);
echo $match;
or something, look into those functions.
Posted: Sun Aug 18, 2002 7:35 pm
by Silver_Eclipse
ok i understand that code somewhat, it makes since, would just like to know what ereg means?
Posted: Sun Aug 18, 2002 7:38 pm
by Silver_Eclipse
err wait how would that script know which line to choose because there may be more lines with <b> in them?
Posted: Sun Aug 18, 2002 8:15 pm
by lc
could be done even simpler.
Code: Select all
$file = file("yourfile.html");
print "$fileї2]";
that would print line 3 (remember php strarts counting at 0) of the html
or even
Code: Select all
$file = file("yourfile.html");
while(list($arr) = each($file)){
if ($arr > "5" and $arr < "25"){
print "$fileї$arr]";
}
}
would print out all lines between 5 and 25. Oh somehow at times I need to use the reverse of the < and > to get the right results on my system... don't ask me why.
hope that helped
Posted: Sun Aug 18, 2002 8:19 pm
by Silver_Eclipse
ok that helps but if it is html then how would i get it to remove the tags and just print the text between them? i trid fgetss but it keeps stripping everything and not just the tags.
Posted: Sun Aug 18, 2002 8:32 pm
by Silver_Eclipse
new problem, its returning only one letter from the line and not the whole line itself
Posted: Sun Aug 18, 2002 8:36 pm
by lc
Look up the strip_tags function on
http://www.php.net
and which script are you using that only returns one character?
Posted: Sun Aug 18, 2002 8:43 pm
by hob_goblin
Code: Select all
$file = "file.txt"
$contents = file($file);
foreach($contents as $value){
if(ereg("<b>(.*)</b>", $value, $matches)){
echo $matchesї1];
}
}
would loop through the whole file and print all bolded statements.
Posted: Sun Aug 18, 2002 8:46 pm
by Silver_Eclipse
do you know how i would do it lc's way? i like the idea of printing lines instead of ur way with the statements in a certain tag because i have other things in the same tag but i dont want them to show. and i can't get it to print the whole line it jsut prints a letter from that line.
Posted: Sun Aug 18, 2002 8:54 pm
by Silver_Eclipse
ok got it to work now just need to find a way to strip the html tags off of it
Posted: Sun Aug 18, 2002 9:09 pm
by volka
Code: Select all
<?php
$file = "file.txt";
$contents = file($file);
foreach($contents as $value)
print(strip_tags($value));
?>
or
Code: Select all
<?php
$file = "maps.out.html";
$fd = fopen($file, "r");
while($line = fgetss($fd, 512))
print(strip_tags($line));
// or something like $contentsї] = strip_tags($line);
?>
manual:
strip_tags
fopen
fgetss
Posted: Sun Aug 18, 2002 11:58 pm
by Silver_Eclipse
ok i have figured out most of what i am trying to do but i cant figure out how to stop my array.
Posted: Mon Aug 19, 2002 12:03 am
by Silver_Eclipse
ok figured it out
Posted: Mon Aug 19, 2002 1:31 pm
by Silver_Eclipse
ok so im almost done with my script but could someone tell me how i could delete specific lines from a file and add new ones in their place?