If there are images, display only teh first one !

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PHPeanuts
Forum Newbie
Posts: 10
Joined: Wed Jan 21, 2009 7:46 pm

If there are images, display only teh first one !

Post by PHPeanuts »

Hi guys,

I'm pretty new with PHP, and all I've learned I did it through the blogging platform WordPress. I hope I'll keep in this beyond WordPress, because it's pretty attracting to me.

By now I have the following issue : when I call for a post, I get, as usual, all of the images I attached to it. But I'd like to retrieve only the first one.

I found this code :

Code: Select all

 
<?php
            $szPostContent = $post->post_content;
            $szSearchPattern = '~<img [^\>]*\ />~';
            preg_match_all( $szSearchPattern, $szPostContent, $aPics );
            $iNumberOfPics = count($aPics[0]);
            if ( $iNumberOfPics > 0 ) {
            for ( $i=0; $i < $iNumberOfPics ; $i++ ) {
            echo $aPics[0][$i];
            };
            };
?>
But it gets me ALL of the images (even if with it I can forget about the text, which is the good new to me).

I'm sure it's a question of syntax : how can I manage those pretty '>' and '0' and 'i++' to get just the "first" image ?
Thanks very much if you can help.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: If there are images, display only teh first one !

Post by Burrito »

use the next dimension in the matched array.

If you only need the first one, you can just use preg_match() instead of preg_match_all()
PHPeanuts
Forum Newbie
Posts: 10
Joined: Wed Jan 21, 2009 7:46 pm

Re: If there are images, display only teh first one !

Post by PHPeanuts »

Hi Burrito, thanks for the reply. However I'm too much bloated actually : how should I get the next dimension ? Then, in PHP.net I still get easily lost.
All I got is exclude the last image if I change the line #7 from 0 to 1, or allow only the posts with one single image to be shown if in the same line I change the sign '>' to '<' and I increase the value to 2.

Please I need some code to understand what you mean :)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: If there are images, display only teh first one !

Post by Burrito »

Code: Select all

 
             $szPostContent = "<img src=\"images/whatever.jpg\"><img src=\"images/blah.jpg\">";
             $szSearchPattern = '~<img [^\>]+>~';
             preg_match( $szSearchPattern, $szPostContent, $aPics );
             echo "<pre>";
             print_r($aPics);
             echo "</pre>";
 
PHPeanuts
Forum Newbie
Posts: 10
Joined: Wed Jan 21, 2009 7:46 pm

Re: If there are images, display only teh first one !

Post by PHPeanuts »

Thanks very much it worked

Here is my final code :

Code: Select all

 
<?php
            $szPostContent = $post->post_content;
            $szSearchPattern = '~<img [^\>]*\ />~';
            preg_match( $szSearchPattern, $szPostContent, $aPics );
            echo $aPics[0];
?>
Post Reply