Forum Moderators: coopster

Message Too Old, No Replies

How do you use ereg() to obtain multiple results?

         

smithaa02

4:33 pm on Apr 5, 2006 (gmt 0)

10+ Year Member



I need to find all id's inserted into a collection of html files and am wondering how to do it. For example, my files may contain:

<a href='/display.php?id=44'>link 1</a> ...html stuff... <a href='/display.php?id=1101'>link 1</a> ...html stuff... <a href='/display.php?id=999'>link 1</a>etc...

What I would hope to get as a result would be 44,1101,999.

I use the key display\.php\?id=([^"]+)" with ereg(), but I only get the first result. How would I get all the id's for each html file?

Little_G

5:43 pm on Apr 5, 2006 (gmt 0)

10+ Year Member



Hi,

try using preg_match_all [uk.php.net]

Andrew

jenningsdev

7:24 pm on Apr 5, 2006 (gmt 0)

10+ Year Member



If you use preg_match_all -

Try getting the values you want, matching them with preg_match_all, looping throug the preg_match_all array using a for loop to access the two dimensional array, and pushing the results into a one dimensional array. You can now use the values by accessing your $ID_array.

Play with the example. Let me know if you need any help

//create an un initialized array
$ID_array = array();

//Chnage the regex [A-Z_a-z] to match your needs
if(preg_match_all('/\{[A-Z_a-z]+\}/',$id, $id_sort)){

//count the number of matches
$numTags = count($id_sort[0]);

//loop through the matches with a for loop
for($i = 0; $i < $numTags; $i++){

//create a variable so you can push the results into your array
$setIdName = $id_sort[0][$i];
array_push($ID_array, $setIdName);

}//end Preg for Loop
}//end if

[edited by: coopster at 10:41 pm (utc) on April 5, 2006]
[edit reason] no email sigs please and thanks :-) [/edit]

coopster

11:33 pm on Apr 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, jenningsdev.

I think we could even make that easier, given the pattern supplied by smithaa02 as well as the sage advice of Little_G to use a perl compatible regular expression match. The pattern is the key, of course, so let's focus on that. We want to match the URI including the named variable

id
in the query string followed by the equal sign. After that we are looking for the value. We know that the value will be one or more of anything that is not a single quotation mark, double quotation mark, space character, ampersand (which might signify another name/value pair in the query string), or the greater than sign (>), which is the closing of our <a element>. We want to stop capturing on any of those in that list as well so as soon as we hit one, our capturing subpattern ends. The /i modifier just tells the regex engine that we don't care if it is uppercase or lowercase that we match.
$pattern = "/display\.php\?id=([^'\"\s&>]+)['\"\s&>]/i"; 
preg_match_all($pattern, $string, $matches);
print implode(',', $matches[1]);

$matches[1] will be the array index that matches the captured subpatterns (the stuff marked by the opening and closing parentheses), $matches[0] always contains the matches from the FULL pattern, each set of subpatterns after that gets it's own new index in the $matches array.

implode just takes that array and makes it into a string for us, separated by commas as smithaa02 requested.

jenningsdev

11:36 pm on Apr 5, 2006 (gmt 0)

10+ Year Member



Yes, that is great idea. Imploding works very well.

smithaa02

4:12 pm on Apr 6, 2006 (gmt 0)

10+ Year Member



I've gotten it to work with my script. Thanks to everybody that responded.

jenningsdev

4:39 pm on Apr 6, 2006 (gmt 0)

10+ Year Member



Glad it worked for you!