Jump to content

[SOLVED] another preg_match_all() problem


ted_chou12

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/133089-solved-another-preg_match_all-problem/
Share on other sites

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
        )

)

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);

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

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.