ted_chou12 Posted November 17, 2008 Share Posted November 17, 2008 here I have a text: <p class="pagination" align=center>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, $array, PREG_PATTERN_ORDER); But this seems only to give 1 but not 2??? How should I do it? Thanks, Ted. Quote Link to comment Share on other sites More sharing options...
ddrudik Posted November 17, 2008 Share Posted November 17, 2008 It depends how the source string might vary, you might consider: <?php $sourcestring="your source string"; preg_match_all('~<p class="pagination" align=center>nbsp; <a [^>]*>([^<]*)</a>[^<]*<a [^>]*>([^<]*)~',$sourcestring,$matches); echo "<pre>".print_r($matches,true); ?> $matches Array: ( [0] => Array ( [0] => <p class="pagination" align=center>nbsp; <a href="link.html">1</a>text in between <a href="link.html>2 ) [1] => Array ( [0] => 1 ) [2] => Array ( [0] => 2 ) ) Quote Link to comment Share on other sites More sharing options...
.josh Posted November 17, 2008 Share Posted November 17, 2008 damn ted, you sure do have a lot of regex questions. maybe you should just buck up and read a regex tutorial or two. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted November 17, 2008 Author Share Posted November 17, 2008 yeah, I should, but there are so much to learn. ddrudik, sorry for not clarifying, the nbsp; should be replaced with any variable text in between. Thanks again, Quote Link to comment Share on other sites More sharing options...
ddrudik Posted November 17, 2008 Share Posted November 17, 2008 If the can be any non-tag text: preg_match_all('~<p class="pagination" align=center>[^<]*<a [^>]*>([^<]*)</a>[^<]*<a [^>]*>([^<]*)~',$sourcestring,$matches); If the can be any non-a href tag text (less preferred): preg_match_all('~<p class="pagination" align=center>.*?<a [^>]*>([^<]*)</a>[^<]*<a [^>]*>([^<]*)~s',$sourcestring,$matches); Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted November 17, 2008 Share Posted November 17, 2008 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 Quote Link to comment Share on other sites More sharing options...
corbin Posted November 17, 2008 Share Posted November 17, 2008 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. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted November 21, 2008 Author Share Posted November 21, 2008 pretty true Quote Link to comment 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.