ted_chou12 Posted November 28, 2009 Share Posted November 28, 2009 Hi, I have multiple content that i wish to capture, so im using preg match all: $content1 = '......<a href="/users/blog.php?user=ted_chou12&id=48" class="readmore">Read more</a> <a href="/users/blog.php?user=ted_chou12&id=48#comments" class="comments">Comments (0)</a> ....<a href="/users/blog.php?user=ted_chou12&id=47" class="readmore">Read more</a> <a href="/users/blog.php?user=ted_chou12&id=47#comments" class="comments">Comments (0)</a>... <a href="/users/blog.php?user=ted_chou12&id=46" class="readmore">Read more</a> <a href="/users/blog.php?user=ted_chou12&id=46#comments" class="comments">Comments (0)</a>........... preg_match_all("/<a\shref=\"\/users\/blog\.php\?user=ted_chou12\&id=(.*?)\"\sclass=\"readmore\">Read\smore<\/a>/s", $content1, $pagearray, PREG_PATTERN_ORDER); I wish to get the id numbers into an array, but this code only seems to get one: ideal output: 48,47,46... real 48 (only) so how should I change it to output all the id numbers in the content, Thanks, Ted Quote Link to comment https://forums.phpfreaks.com/topic/183209-preg-match-all/ Share on other sites More sharing options...
salathe Posted November 28, 2009 Share Posted November 28, 2009 Change the (.*?) to (\d+) The reason is because your capturing group (that which needs changed) was grabbing more than you want it to. For example, here's your string with the original matches highlighted: Key: no match, match, group 1. [pre] ......<a href="/users/blog.php?user=ted_chou12&id=48" class="readmore">Read more</a> <a href="/users/blog.php?user=ted_chou12&id=48#comments" class="comments">Comments (0)</a> ....<a href="/users/blog.php?user=ted_chou12&id=47" class="readmore">Read more</a> <a href="/users/blog.php?user=ted_chou12&id=47#comments" class="comments">Comments (0)</a>... <a href="/users/blog.php?user=ted_chou12&id=46" class="readmore">Read more</a> <a href="/users/blog.php?user=ted_chou12&id=46#comments" class="comments">Comments (0)</a>.... [/pre] Compare that with using (\d+): [pre] ......<a href="/users/blog.php?user=ted_chou12&id=48" class="readmore">Read more</a> <a href="/users/blog.php?user=ted_chou12&id=48#comments" class="comments">Comments (0)</a> ....<a href="/users/blog.php?user=ted_chou12&id=47" class="readmore">Read more</a> <a href="/users/blog.php?user=ted_chou12&id=47#comments" class="comments">Comments (0)</a>... <a href="/users/blog.php?user=ted_chou12&id=46" class="readmore">Read more</a> <a href="/users/blog.php?user=ted_chou12&id=46#comments" class="comments">Comments (0)</a>.... [/pre] Quote Link to comment https://forums.phpfreaks.com/topic/183209-preg-match-all/#findComment-966937 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.