Forum Moderators: coopster

Message Too Old, No Replies

Find one line in a file?

How to get one line out of a file with php

         

TellJane

7:27 pm on Oct 10, 2004 (gmt 0)

10+ Year Member



hello everybody

my first post! :)

Im trying to do somthing i think is simple but i dont even know where to start

I want to look at a variable (an html file in a a variable) and extract a line that will look like this)

<form method="post" action="mysite.com/upload.php" name="member_stat" [maybe some more bits here]>

the only thing that *never* changes is the 'name="member_stat"' bit

How can i get this entire line into a variable? i have looked at string functions, cant find anything and im not very good at regular expressions but surely it cant be that hard?

thankyou in advance!

jusdrum

9:08 pm on Oct 10, 2004 (gmt 0)

10+ Year Member



Try look at fopen, fread, etc. on php.net's online documentation.

Basically you will open the file, read it's contents, and look for the line that contains what you are looking for and storing that line in a variable.

TellJane

7:26 am on Oct 11, 2004 (gmt 0)

10+ Year Member



Thankyou, but you misread me :)

I already have the html file in a variable. NO problem whatsoever.

Im just stumped as to how to extract that one line :(

mincklerstraat

8:12 am on Oct 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey Tell Jane, welcome to webmasterworld. [webmasterworld.com].

Sorry for the bad news, but you're going to have to regex this one, since it's not a simple find, but one that's also supposed to 'begin' at a place that's not inside the find string itself.


$check = preg_match('#(<[^>]*name="member_stat"[^>]*>)#', $matches);
if($check) $caughtstring = $matches[1];
else $caughtstring = 'doesn\'t seem to be here';

Regexes can be difficult to learn at first but are groovy.

TellJane

9:03 am on Oct 11, 2004 (gmt 0)

10+ Year Member



thankyou mincklerstraat!

do you know anywhere that is good to learn regex for a regex newbie? im not a php newbie but ive always shied away from regex because it scares me lol!

its probably time i started learning, i do hate to have to ask people to do my job for me :(

thankyou again!

mincklerstraat

9:10 am on Oct 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The url that's usually dropped here is [regular-expressions.info...] . Once you've chewed through your tutorial of choice, I find php.net's page [be2.php.net...] very handy as a recap for what all these things mean - skip the 'differences from perl' part - this refreshes what ^, $, /s, etc mean.

Another hint: learn the preg_ expressions and not the ereg ones, preg is faster and more flexible, ereg is 'older.'

happy regexing!

jatar_k

2:41 pm on Oct 11, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



there's also this
Regular Expressions [webmasterworld.com]

TellJane

8:53 pm on Oct 11, 2004 (gmt 0)

10+ Year Member



great!

thanks very much jatar_k and mincklerstraat, thats most appreciated. the website looks especially good!