Jump to content

preg_match_all but in one line?


vin_akleh

Recommended Posts

i can match this pattern

/<a href="\/How_I_Met_Your_Mother\/season-1\/episode-1\/search\?page=\d+".+\d+<\/a>/i

but my priblem is that this pattern selects line by line into an array

what i need it to do is that it selects every match in 1 single line and puts it in a array with different index.

 

$html=<div class="pagination"><span class="disabled prev_page">« Previous</span> <span class="current">1</span> <a href="/How_I_Met_Your_Mother/season-1/episode-1/search?page=2" rel="next">2</a> <a href="/How_I_Met_Your_Mother/season-1/episode-1/search?page=3">3</a> <a href="/How_I_Met_Your_Mother/season-1/episode-1/search?page=4">4</a> <a href="/How_I_Met_Your_Mother/season-1/episode-1/search?page=2" class="next_page" rel="next">Next »</a></div>

//*this is all in one line not multiple lines*
//if this was in multiple lines i wouldn't have this problem

preg_match_all('/<a href="\/How_I_Met_Your_Mother\/season-1\/episode-1\/search\?page=.*/', $html, $pages);
preg_match_all('#<a.+href="/How_I_Met_Your_Mother/season-\d+/episode-\d+/search\?page=\d+.+?">[^<]+</a>#', $html, $pages);
for ($i=0;$i<sizeof($pages[0]);$i++)
{
	echo $pages[0][$i];
	echo $pages[0][0];
	echo $pages[0][1]; //////////////////////////////////////////this would give undefined index
}

 

any suggestions would be appreciated.

Link to comment
Share on other sites

Wrap parentheses around the text your wanting to capture

preg_match_all('#(<a href="/How_I_Met_Your_Mother/season-\d+/episode-\d+/search\?page=\d".*?>[^<]+</a>)#', $html, $pages);

 

print_r($pages) returns

Array
(
    [0] => Array
        (
            [0] => <a href="/How_I_Met_Your_Mother/season-1/episode-1/search?page=2" rel="next">2</a> 
            [1] => <a href="/How_I_Met_Your_Mother/season-1/episode-1/search?page=3">3</a> 
            [2] => <a href="/How_I_Met_Your_Mother/season-1/episode-1/search?page=4">4</a> 
            [3] => <a href="/How_I_Met_Your_Mother/season-1/episode-1/search?page=2"class="next_page" rel="next">Next »</a> 
        )

    [1] => Array
        (
            [0] => <a href="/How_I_Met_Your_Mother/season-1/episode-1/search?page=2" rel="next">2</a> 
            [1] => <a href="/How_I_Met_Your_Mother/season-1/episode-1/search?page=3">3</a> 
            [2] => <a href="/How_I_Met_Your_Mother/season-1/episode-1/search?page=4">4</a> 
            [3] => <a href="/How_I_Met_Your_Mother/season-1/episode-1/search?page=2"class="next_page" rel="next">Next »</a> 
        )

)

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.