php - Retrieving & displaying images from a post with preg_match -
basically piece of code grab first image of post , display @ page. if no images, show default image.
how modify make display max 4 images ?
<?php $imagesrc = c('plugin.indexpostimage.image','/images/default.png'); preg_match( '#\<img.+?src="([^"]*).+?\>#s', $sender->eventarguments['post']->body, $images ); if ($images[1]) { $imagesrc = $images[1]; } $thumbs ='<a class="indeximg" href="'. $sender->eventarguments['post']->url .'">'. img($imagesrc, array('title'=>$sline,'class'=>"indeximg")).'</a>'; echo "$thumbs";
your found images in array $images
according to preg_match manual:
int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )
$matches[1]
have text matched first captured parenthesized subpattern, , on.
so should iterate on $images
array 1 5 , if $images[$i]
not empty add image thumbs. try this:
<?php $imagesrc = c('plugin.indexpostimage.image','/images/default.png'); preg_match( '#\<img.+?src="([^"]*).+?\>#s', $sender->eventarguments['post']->body, $images ); $thumbs = ""; ($i = 1; $i <= 5; $i) { if(!empty($images[$i])) { $thumbs .= '<a class="indeximg" href="' . $sender->eventarguments['post']->url . '">' . img($images[$i], array('title' => $sline, 'class' => "indeximg")) . '</a>'; } else { break; } } echo "$thumbs";
Comments
Post a Comment