Results 1 to 8 of 8

Thread: Help using preg_match to get number of pages (of <a href="javascript: GotoPage(4);" .

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Help using preg_match to get number of pages (of <a href="javascript: GotoPage(4);" .

    Hi all i am trying to use preg_match to get number of pages from a html that has this format :

    of <a href="javascript: GotoPage(4);" class="pagingLink">4</a>

    in this case i want to grab the number 4 but it never works. Could any one tell me what i am doing wrong ?Hope some one help me .Thanks




    PHP Code:

    global $curl;

    // Retrieve number of pages page.
        
    echo 'Checking friends list... ';
        
    curl_setopt($curlCURLOPT_URL'./test.html');
        
    curl_setopt($curlCURLOPT_POST0);
        
    $result curl_exec($curl);
        
    $headers curl_getinfo($curl);
        
        
    // Find out how many pages there are. 
        
    if (preg_match('~of <a href="(.+?)" class="pagingLink">([0-9]+)</a>~'$result$matches))
        {
            
    $pages $matches[2];
                echo 
    ' Number of pages: $pages ';
            unset(
    $matches);
            
        } 
    Html content :
    HTML Code:
    of <a href="javascript: GotoPage(4);" class="pagingLink">4</a>
    Last edited by tony007; Jun 15th, 2008 at 06:35 AM.

  2. #2
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: Help using preg_match to get number of pages (of <a href="javascript: GotoPage(4);" .

    That works fine here. Are you sure cURL is getting the data?
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: Help using preg_match to get number of pages (of <a href="javascript: GotoPage(4);" .

    thanks for your reply. I used print $result; and yes it loaded that remote page. Try to add this url (http://friends.myspace.com/index.cfm...ID==140771218)instead test.html and you can see the full html .Hope i get help getting the number of pages on html of that page.Thanks

  4. #4
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: Help using preg_match to get number of pages (of <a href="javascript: GotoPage(4);" .

    HTML Code:
    <span class="pager_container"><div class="friends_bot" id="nav_bottom"><span class="nav_left"><span class="pgerTitle">Listing 1-40 of 8,702</span><a class="highlight">1</a> <a href="javascript: GotoPage(2);" class="pagingLink">2</a> <a href="javascript: GotoPage(3);" class="pagingLink">3</a> <a href="javascript: GotoPage(4);" class="pagingLink">4</a> <a href="javascript: GotoPage(5);" class="pagingLink">5</a> <a href="javascript: GotoPage(6);" class="pagingLink">&raquo;</a> of <a href="javascript: GotoPage(218);" class="pagingLink">218</a> </span><span class="nav_right"><a class="prevPagingLink disabledPaging">prev</a>  | <a href="javascript: GotoPage(2);" class="nextPagingLink">next</a> </span></div></span>
    Where on earth did 'of' come from?
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: Help using preg_match to get number of pages (of <a href="javascript: GotoPage(4);" .

    Thanks for your reply. If you look at your html code that you posted you will find 'of' just before <a hreft= ... almost at the end of the code. But any ways if you look at that page in your browser you see some thing like 1 2 3 4 5 &#187; of 218 prev | next . I wanted to get that 218 but for some reason my doesn't it get the corect value both in firbox and ID :-(
    http://friends.myspace.com/index.cfm...ndID=140771218
    Last edited by tony007; Jun 15th, 2008 at 07:33 AM.

  6. #6
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: Help using preg_match to get number of pages (of <a href="javascript: GotoPage(4);" .

    PHP Code:
    $result 'blahblahblah <span class="pager_container"><div class="friends_bot" id="nav_bottom"><span class="nav_left"><span class="pgerTitle">Listing 1-40 of 8,702</span><a class="highlight">1</a> <a href="javascript: GotoPage(2);" class="pagingLink">2</a> <a href="javascript: GotoPage(3);" class="pagingLink">3</a> <a href="javascript: GotoPage(4);" class="pagingLink">4</a> <a href="javascript: GotoPage(5);" class="pagingLink">5</a> <a href="javascript: GotoPage(6);" class="pagingLink">&raquo;</a> of <a href="javascript: GotoPage(218);" class="pagingLink">218</a> </span><span class="nav_right"><a class="prevPagingLink disabledPaging">prev</a>  | <a href="javascript: GotoPage(2);" class="nextPagingLink">next</a> </span></div></span>';
    if (
    preg_match('%of <a href="(.+?)" class="pagingLink">([0-9]+)</a>%'$result$matches)) {
        
    $pages $matches[2];
        echo 
    'There are '.$pages.' pages.';

    Outputs:
    Code:
    There are 218 pages.
    That works fine for me...

    I also tried it with a heredoc of the full page and it worked fine too.

    EDIT: I couldn't test cURL but this works too:
    PHP Code:
    <?php
    $result
    file_get_contents("http://friends.myspace.com/index.cfm?fuseaction=user.viewfriends2&friendID=140771218");
    if (
    preg_match('%of <a href="(.+?)" class="pagingLink">([0-9]+)</a>%'$result$matches)) {
        
    $pages $matches[2];
        echo 
    'There are '.$pages.' pages.';
    }
    ?>
    Last edited by RudiVisser; Jun 15th, 2008 at 08:23 AM.
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: Help using preg_match to get number of pages (of <a href="javascript: GotoPage(4);" .

    RudiVisser many thanks for helping me. I tried your first code and it worked for the give result sample data. I tried your second code as it is and number of pages never didn't output!! Infact it scaped the the if statment since even the echo "there are didn't get printed!!


    i even used print $result; after $result = curl_exec($curl); and i saw the that page get loaded!! so i concluded that result has the right html!



    I am runing this in Apache/1.3.33 (Win32) with PHP version :5.0.4 in local server. Do you think some thing is wrong with version of apache or php ? Hope you help me fix this problem.Thanks
    Last edited by tony007; Jun 15th, 2008 at 03:41 PM.

  8. #8
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: Help using preg_match to get number of pages (of <a href="javascript: GotoPage(4);" .

    I highly doubt it, but you are using very old software.

    When you say pages never outputted, did you try adding an else { }, remember the page could have just been taking a while to load.
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width