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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.