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.
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.
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.
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.
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
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:
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 ?
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?
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.