Forum Moderators: coopster

Message Too Old, No Replies

preg_match and store resulting string ...

once i've found it, how do i stick it in a string?

         

bobhoskins

4:39 pm on May 17, 2005 (gmt 0)

10+ Year Member



Hi,

i want to go through a file and find every occurance of a certain start tag, then put the contents following that indicator into a string up to the end tag.

eg storing a list of all links on a web page

i can use preg_match to find the start tag but then am a bit stumped after that ...

any ideas?

killroy

11:26 pm on May 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First, write a regex that matches the entire patter: start tag, text inbetween, end tag. Then ensure that then inside part gets captured with ( and ). Finally use preg_match_all and a result variable to capture all matches.

Simplified example:

preg_match_all('/<a href="[^"]*">([^<]*)<\/a>/i',$htmlText,&$result);

Print_r($result);

$result[0] should be an array of all full matches, $result[1] an array of all first subpattern matches and so on.

SN