Results 1 to 10 of 10

Thread: I need a preg_match pattern

  1. #1
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default I need a preg_match pattern

    I have a site that have these lines involved:

    HTML Code:
    ...
    <div class="post_icon">
    	<div class="post_icon_overlay"> </div>
    	<img src="http://localhost/wp/65a39792ad0fc80.jpg">
    </div>
    ...
    I have made a function that is trying to grab the img address. But I cannot make a good pattern for it.

    PHP Code:
    function GetImg($pageurl){
       
    $contents = @file_get_contents($pageurl);
       if (!
    $contents) return FALSE;
       
    preg_match(' ??? '$contents$matches);
       return 
    $matches[1];

    can any one help?

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    If that is the exact pattern and that is a tab i think this should do it..

    PHP Code:
    '/.*?class="post_icon".*?\t<img src="(.*?)">/s' 
    Corrections to my coding/thoughts welcome.

  3. The Following User Says Thank You to bluewalrus For This Useful Post:

    mda_omega (10-31-2010)

  4. #3
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by bluewalrus View Post
    If that is the exact pattern and that is a tab i think this should do it..

    PHP Code:
    '/.*?class="post_icon".*?\t<img src="(.*?)">/s' 
    Works. thank you.

  5. #4
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default all images in a url

    what about all images that has the same prefix url ?

    Code:
    http://localhost/2450/
    I cannot change the function, may you please change it for me? these are examples

    Code:
    http://localhost/2450/ss_3ba0cf8efe4c69a96fe6.600x338.jpg?t=1288373899
    http://localhost/2450/sfeegfba0cf84c69a9erf6fe6.600x338.jpg?t=1288373899
    http://localhost/2450/ss_3gfef84ce6efgrg4et46.600x338.jpg?t=1288373899
    http://localhost/2450/ssk_3bafwef0cfuc69a96fe6.600x338.jpg?t=1288373899
    Last edited by mda_omega; 10-31-2010 at 10:44 AM.

  6. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,211
    Thanks
    96
    Thanked 101 Times in 99 Posts

    Default

    can we see the image addresses in context? We need to see what separates one image address from another. Worded another way: what does the code look like where at least one of the image addresses is located.

    bluewalrus, why can't the first pattern be simplified just a wee bit to <img src="(.*?)">?
    To choose the lesser of two evils is still to choose evil. My personal site

  7. The Following User Says Thank You to james438 For This Useful Post:

    mda_omega (11-01-2010)

  8. #6
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    The first pattern was to pull the images contained in post_icon.

    I think per the new requirement this should work. If not we'd need to see the actual code.

    PHP Code:
    '/<img src="(http:\/\/localhost\/2450\/.*?)">/' 
    Corrections to my coding/thoughts welcome.

  9. The Following User Says Thank You to bluewalrus For This Useful Post:

    mda_omega (11-01-2010)

  10. #7
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,211
    Thanks
    96
    Thanked 101 Times in 99 Posts

    Default

    yes, but that is not what he was asking for. or at least I am not seeing that in his initial post.
    Either way, I would like to see the actual code mda_omega.
    To choose the lesser of two evils is still to choose evil. My personal site

  11. #8
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    you both were right I needed something between what you said. all the images that has the same address. I think I should use preg_match_all instead. Am I right?

    PHP Code:
       $contents = @file_get_contents($pageurl);
       if (!
    $contents) return FALSE;
       
    preg_match_all('/<img src="(http:\/\/localhost\/2450\/.*?)">/'$contents$matches); 
    So. the other problem is. How can I reach them one by one?

    is it good?

    PHP Code:
    echo $matches[0];
    echo 
    $matches[1];
    echo 
    $matches[2];
    ... 
    I dont need to get errors, so, how can I figure out how many they are? Do I need a loop to use them one by one??

  12. #9
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    You can use the foreach function

    http://php.net/manual/en/control-structures.foreach.php

    so it'd be

    PHP Code:
    foreach ($matches as $value) {
        echo 
    $value;

    assuming that array has all the values you want.
    Corrections to my coding/thoughts welcome.

  13. The Following User Says Thank You to bluewalrus For This Useful Post:

    mda_omega (11-01-2010)

  14. #10
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    thank you thank you thank you :xxx

    That was simple and effective.

Bookmarks

Posting Permissions

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