Reading From Files

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

Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Reading From Files

Post 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?
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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.
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

ok i understand that code somewhat, it makes since, would just like to know what ereg means?
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

err wait how would that script know which line to choose because there may be more lines with <b> in them?
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post by lc »

could be done even simpler.

Code: Select all

$file = file("yourfile.html");

print "$file&#1111;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))&#123;

if ($arr > "5" and $arr < "25")&#123;
print "$file&#1111;$arr]";
&#125;

&#125;
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
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post 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.
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

new problem, its returning only one letter from the line and not the whole line itself
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post by lc »

Look up the strip_tags function on http://www.php.net

and which script are you using that only returns one character?
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Code: Select all

$file = "file.txt"
$contents = file($file);
foreach($contents as $value)&#123;
if(ereg("<b>(.*)</b>", $value, $matches))&#123;
 echo $matches&#1111;1];
 &#125; 
&#125;
would loop through the whole file and print all bolded statements.
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post 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.
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

ok got it to work now just need to find a way to strip the html tags off of it
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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&#1111;] = strip_tags($line);
?>
manual:
strip_tags
fopen
fgetss
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post 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.
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

ok figured it out
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

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