Results 1 to 4 of 4

Thread: How to filter preg_match_all result

  1. #1

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

    How to filter preg_match_all result

    Hi all .i am using the following code to get the image thumb and url from a remote html .The code is working but the problem is that it outputs some unwanted images !! could any one help me so i can output only those images that are like the give html and omit the rest.Thanks


    PHP Code:
    $photos "http://www.somesite.com/images.html";
        
        
    curl_setopt($chCURLOPT_URL$photos);
        
    curl_setopt($chCURLOPT_POST0);
        
    $pagecontents curl_exec($ch);

     

            
    $pattern "/src=[\"‘]?([^\"’]?.*(png|jpg|gif))[\"’]?/i";
            
    preg_match_all($pattern$pagecontents$matches);
        
        
    $count count($matches[1]);
            
            echo 
    $count

    html of images to consider:
    HTML Code:
       <div class="photo_index_item">
                                <div id="dvImgListHypImage">
                                <a id="ctl00_cpMain_UserViewPictureControl_ImageListings1_dlImageList_ctl14_hypImage" title="image name" class="photo_image" href="http://www.somsite.com/imagelink2"><img title="image name" src="http://www.somesite.com/m_d1288db7a7c2ab5c34b521ac62eb3e43.jpg" style="border-width:0px;" /></a>
                                </div>

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

    Re: How to filter preg_match_all result

    PHP Code:
    foreach($matches as $key => $match) {
        if(!
    preg_match("the html you want to match"$match)) {
            unset(
    $matches[$key]);
        }

    » 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: How to filter preg_match_all result

    Rudi thanks for your reply. could you explain to me how it works ? i don't know what you mean by the html you want ot match ? All i want to collect http://www.somesite.com/m_d1288db7a7...ac62eb3e43.jpg

    only when it appears with in <div> only and omit the rest of images that are outside of <div>

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

    Re: How to filter preg_match_all result

    HTML Code:
       <div class="photo_index_item">
                                <div id="dvImgListHypImage">
                                <a id="ctl00_cpMain_UserViewPictureControl_ImageListings1_dlImageList_ctl14_hypImage" title="image name" class="photo_image" href="http://www.somsite.com/imagelink2"><img title="image name" src="http://www.somesite.com/m_d1288db7a7c2ab5c34b521ac62eb3e43.jpg" style="border-width:0px;" /></a>
                                </div>
    Well you posted this so I assume you mean this.

    I don't see why you just don't alter your original expression to have the containing <div> though.
    » 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