Author Topic: [SOLVED] another preg_match_all() problem  (Read 793 times)

0 Members and 1 Guest are viewing this topic.

Offline ted_chou12Topic starter

  • Devotee
  • Posts: 1,481
  • Gender: Male
  • Hitman Hits
    • View Profile
    • NetFriending
[SOLVED] another preg_match_all() problem
« on: November 17, 2008, 12:08:25 PM »
here I have a text:
Code: [Select]
<p class="pagination" align=center>nbsp;&nbsp;<a href="link.html">1</a>text in
between <a href="link.html>2</a>text....</p>
How do I get the 1 and 2 in an array?
what I am trying is:

preg_match_all
("/<p\sclass=\"pagination\"\salign=center>.*?<a\shref=\".*?\">(\d)<\/a>.*?<\/p>/s"$content$arrayPREG_PATTERN_ORDER);

But this seems only to give 1 but not 2???
How should I do it?
Thanks,
Ted.
Currently working on a website called NetFriending.co.cc, it is a free social networking site, features such as guestbook, blogs, are fully customizable in NetFriending, also has a forum to help you get to know others, if you are interested in the site, the url is: NetFriending.co.cc Hope everyone can get involved because the site is still quite new, so not much people are there at the moment, please feel free to leave a comment there or send a message to our staff. Register Here The link above is for registeration, it is 100% Free and Safe, everyone there is friendly and you can build your network. Photo sharing upload your own photos as well!!!

Offline ddrudik

  • Enthusiast
  • Posts: 78
    • View Profile
    • myregextester.com
Re: another preg_match_all() problem
« Reply #1 on: November 17, 2008, 12:14:16 PM »
It depends how the source string might vary, you might consider:
Code: [Select]
<?php
$sourcestring
="your source string";
preg_match_all('~<p class="pagination" align=center>nbsp;&nbsp;<a [^>]*>([^<]*)</a>[^<]*<a [^>]*>([^<]*)~',$sourcestring,$matches);
echo 
"<pre>".print_r($matches,true);
?>


$matches Array:
(
    [0] => Array
        (
            [0] => <p class="pagination" align=center>nbsp;&nbsp;<a href="link.html">1</a>text in
between <a href="link.html>2
        )

    [1] => Array
        (
            [0] => 1
        )

    [2] => Array
        (
            [0] => 2
        )

)

Offline .josh

  • Administrator
  • 'Insane!'
  • *
  • Posts: 13,159
  • Grumpy Old Man
    • View Profile
Re: another preg_match_all() problem
« Reply #2 on: November 17, 2008, 12:19:35 PM »
damn ted, you sure do have a lot of regex questions.  maybe you should just buck up and read a regex tutorial or two.

Did I help you? Feeling generous? Donate to me! | Donate to phpfreaks!

Offline ted_chou12Topic starter

  • Devotee
  • Posts: 1,481
  • Gender: Male
  • Hitman Hits
    • View Profile
    • NetFriending
Re: another preg_match_all() problem
« Reply #3 on: November 17, 2008, 12:26:54 PM »
yeah, I should, but there are so much to learn.
ddrudik, sorry for not clarifying, the nbsp;&nbsp; should be replaced with any variable text in between.
Thanks again,
Currently working on a website called NetFriending.co.cc, it is a free social networking site, features such as guestbook, blogs, are fully customizable in NetFriending, also has a forum to help you get to know others, if you are interested in the site, the url is: NetFriending.co.cc Hope everyone can get involved because the site is still quite new, so not much people are there at the moment, please feel free to leave a comment there or send a message to our staff. Register Here The link above is for registeration, it is 100% Free and Safe, everyone there is friendly and you can build your network. Photo sharing upload your own photos as well!!!

Offline ddrudik

  • Enthusiast
  • Posts: 78
    • View Profile
    • myregextester.com
Re: another preg_match_all() problem
« Reply #4 on: November 17, 2008, 12:34:27 PM »
If the &nbsp;&nbsp; can be any non-tag text:
Code: [Select]
preg_match_all('~<p class="pagination" align=center>[^<]*<a [^>]*>([^<]*)</a>[^<]*<a [^>]*>([^<]*)~',$sourcestring,$matches);If the &nbsp;&nbsp; can be any non-a href tag text (less preferred):
Code: [Select]
preg_match_all('~<p class="pagination" align=center>.*?<a [^>]*>([^<]*)</a>[^<]*<a [^>]*>([^<]*)~s',$sourcestring,$matches);

Offline nrg_alpha

  • Staff Alumni
  • Addict
  • *
  • Posts: 2,480
  • Gender: Male
  • PHPenchant, I haz it!
    • View Profile
    • Portfolio Site
Re: another preg_match_all() problem
« Reply #5 on: November 17, 2008, 05:02:17 PM »
damn ted, you sure do have a lot of regex questions.  maybe you should just buck up and read a regex tutorial or two.

yeah, I should, but there are so much to learn.

So by this response, this means that instead of reading up on things, doing some tutorials perhaps, it's best to simply post every problem on here and have others solve things? While it is the 'easy way', it's not the 'best way'. I would recommend taking some time out and going through the regex manual.. have a glance at the resources, or even pick up this book, which is amazing at teaching the inner workings of regex.

Otherwise, you're not maximizing your learning of regex. It's all intimidating.. I know.. been there.. done that. But trust me.. once you start to grasp the basics, everything starts to click.. And before you know it, you won't need to rely on others to solve your porblems (unless you're REALLY stuck).

Cheers,

NRG
« Last Edit: November 17, 2008, 05:05:33 PM by nrg_alpha »
"Build a man a fire, you warm him for a day. Set a man on fire, and he'll be warm for the rest of his life."
PHP = One cool palindrome!

Offline corbin

  • Guru
  • Freak!
  • *
  • Posts: 7,951
  • Gender: Male
    • View Profile
Re: another preg_match_all() problem
« Reply #6 on: November 17, 2008, 05:16:44 PM »
To piggyback on nrg_alpha's response, you're the proverbial man being taught to fish, except you refuse to learn, and some day someone isn't going to be there to fish for you ;p.
Why doesn't anyone ever say hi, hey, or whad up world?

Offline ted_chou12Topic starter

  • Devotee
  • Posts: 1,481
  • Gender: Male
  • Hitman Hits
    • View Profile
    • NetFriending
Re: [SOLVED] another preg_match_all() problem
« Reply #7 on: November 21, 2008, 04:11:07 PM »
pretty true :P
Currently working on a website called NetFriending.co.cc, it is a free social networking site, features such as guestbook, blogs, are fully customizable in NetFriending, also has a forum to help you get to know others, if you are interested in the site, the url is: NetFriending.co.cc Hope everyone can get involved because the site is still quite new, so not much people are there at the moment, please feel free to leave a comment there or send a message to our staff. Register Here The link above is for registeration, it is 100% Free and Safe, everyone there is friendly and you can build your network. Photo sharing upload your own photos as well!!!