Get a first image of a post with preg_match_all

Discussion in 'PHP' started by Romik84, Mar 15, 2010.

  1. #1
    Hi there,

    I would need a help with preg_match_all function. I try to get first image of a post for related posts plugin. Currently I use following function:

    function get_first_image() {
     global $post, $posts;
     $first_img = '';
     $output = preg_match_all('/< *img[^>]*src *= *["\']?([^"\']*)/', $post->post_content, $matches);
     $first_img = $matches [1][0];
     return $first_img;
    }
    
    Code (markup):
    This function is working without a problem. The problem is that some posts are not in the wp_posts table so it doesn't show any picture for some items. Some posts contents are in wp_post_settings table created by a plugin where new posts contents are saved. How should the function looks now to work for both (for classic post content in the wp_posts as well as for wp_post_settings.

    Thanks.
     
    Romik84, Mar 15, 2010 IP
  2. mattinblack

    mattinblack Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I wouldnt use preg match for this. Not only is it notoriously slow but it also can behave unpredictably in my experience. I would convert the post to all lower case, do strpos from start to find <img then to find next > starting at that position and cut the string out.
     
    mattinblack, Mar 15, 2010 IP
  3. phpmik

    phpmik Peon

    Messages:
    3
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i would just parse the html into an array, have all the img tags in a easy to access array, and not only would i get the first one, i could get all of them and know how many etc.
     
    phpmik, Mar 15, 2010 IP
    YoGem likes this.
  4. YoGem

    YoGem Active Member

    Messages:
    676
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    90
    #4
    as phpMilk stated, the best way is parsing the html into an array or using an html dom parser (easy to download everywhere) and extracting the first img that you meet, for instance:

    Using PHP HTML 5 DOM PARSER:

    
    			// find all imagea
    			foreach($html->find('img') as $e)
        			                                                  { if (!$manyimage) { $fm = $e->src; echo "<img src=\"$fm\">"; $manyimage = 1; } }
    
    PHP:
    I think it should work.
     
    YoGem, Mar 15, 2010 IP
  5. Romik84

    Romik84 Active Member

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    I don't understand how to implement that :confused:
     
    Romik84, Mar 16, 2010 IP
  6. YoGem

    YoGem Active Member

    Messages:
    676
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    90
    #6
    Ok, go here and download the dom parser >>> simplehtmldom dot sourceforge dot net <<<

    Then try with this simple PHP code and tell me if it work :)

    
    <?php
    include('simple_html_dom.php');
    
    $html = file_get_html('http://www.google.com/');
    // find all images
        foreach ($html->find('img') as $e)
        { if (!$manyimage) { $fm = $e->src; echo "<img src=\"$fm\">"; $manyimage = 1; } }
    ?>
    
    PHP:
    This will show the first image of a website... in this case will be the Google Logo :)
     
    YoGem, Mar 16, 2010 IP
  7. Romik84

    Romik84 Active Member

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #7
    Is possible to show only first image of a tag?
     
    Romik84, Mar 16, 2010 IP
  8. YoGem

    YoGem Active Member

    Messages:
    676
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    90
    #8
    No, with the provided code I said to show only the first one but you can transform it to show all the images:
    
    <?php
    include('simple_html_dom.php');
    
    $html = file_get_html('http://www.google.com/');
    // find all images
        foreach ($html->find('img') as $e)
        { $fm = $e->src; echo "<img src=\"$fm\">"; }
    ?>
    
    PHP:
     
    YoGem, Mar 16, 2010 IP
  9. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #9
    I'm on my gaming system right now so I don't have access to the PHP source code to check, but don't the DOM/XML parsers use regular expressions to parse the input into tokens from beginning to end anyways ?
     
    joebert, Mar 16, 2010 IP
  10. Romik84

    Romik84 Active Member

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #10
    I have no luck with the code. I don't know why. On another forum I posted the question too. I've got answer:

    I have in my database this table: wp_4_post_settings and columns where are the images Product_Image_Small

    so I have tried this code:

    function get_first_image() {
     	global $post, $wpdb;
    	preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/', $post->post_content, $matches);
     	$first_img[] = $matches [1];
    $sql="SELECT Product_Image_Small FROM wp_4_post_settings WHERE post_id=".$post->ID;
    	$result = $wpdb->get_row($sql);
    	preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/', $result->Product_Image_Small, $matches);
    	$first_img[] = $matches [1];
     return $first_img[0];
    }
    Code (markup):
    am I missing something?
     
    Romik84, Mar 20, 2010 IP
  11. mattinblack

    mattinblack Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Yeah as I said before don't use preg match...

    But you might also find that the code you wrote only finds < img < img etc and not <img because you put a space in (< *img and not <*img) thus requiring the image tags be structured < img .... I hate preg match.
     
    mattinblack, Mar 20, 2010 IP