Jump to content

preg match all


ted_chou12

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/183209-preg-match-all/
Share on other sites

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]

Link to comment
https://forums.phpfreaks.com/topic/183209-preg-match-all/#findComment-966937
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.