Reading From Files
Moderator: General Moderators
-
Silver_Eclipse
- Forum Commoner
- Posts: 61
- Joined: Sun Aug 18, 2002 7:26 pm
Reading From Files
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?
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
if its on its own line, and you know that line, you can use file()
say file.txt contains
i could then do this script to pull out 'this is a tag'
or something, look into those functions.
say file.txt contains
Code: Select all
<html>
this is some code
<b>this is a tag</b>
this is some more code
</html>Code: Select all
$filename = "file.txt";
$fd = fopen ($filename, "r");
$contents = fread ($fd, filesize ($filename));
fclose ($fd);
ereg("<b>(.*)</b>", $contents, $match);
echo $match;-
Silver_Eclipse
- Forum Commoner
- Posts: 61
- Joined: Sun Aug 18, 2002 7:26 pm
-
Silver_Eclipse
- Forum Commoner
- Posts: 61
- Joined: Sun Aug 18, 2002 7:26 pm
could be done even simpler.
that would print line 3 (remember php strarts counting at 0) of the html
or even
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
Code: Select all
$file = file("yourfile.html");
print "$fileї2]";or even
Code: Select all
$file = file("yourfile.html");
while(list($arr) = each($file)){
if ($arr > "5" and $arr < "25"){
print "$fileї$arr]";
}
}hope that helped
-
Silver_Eclipse
- Forum Commoner
- Posts: 61
- Joined: Sun Aug 18, 2002 7:26 pm
-
Silver_Eclipse
- Forum Commoner
- Posts: 61
- Joined: Sun Aug 18, 2002 7:26 pm
Look up the strip_tags function on http://www.php.net
and which script are you using that only returns one character?
and which script are you using that only returns one character?
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
Code: Select all
$file = "file.txt"
$contents = file($file);
foreach($contents as $value){
if(ereg("<b>(.*)</b>", $value, $matches)){
echo $matchesї1];
}
}-
Silver_Eclipse
- Forum Commoner
- Posts: 61
- Joined: Sun Aug 18, 2002 7:26 pm
-
Silver_Eclipse
- Forum Commoner
- Posts: 61
- Joined: Sun Aug 18, 2002 7:26 pm
Code: Select all
<?php
$file = "file.txt";
$contents = file($file);
foreach($contents as $value)
print(strip_tags($value));
?>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);
?>strip_tags
fopen
fgetss
-
Silver_Eclipse
- Forum Commoner
- Posts: 61
- Joined: Sun Aug 18, 2002 7:26 pm
-
Silver_Eclipse
- Forum Commoner
- Posts: 61
- Joined: Sun Aug 18, 2002 7:26 pm