Forum Moderators: coopster
<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?
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]
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
idin 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]);
implode just takes that array and makes it into a string for us, separated by commas as smithaa02 requested.