Extact tags from post content

August 20, 2013

/*
	Thanks to: Matt Varone - http://mattvarone.com/wordpress/images-post-wordpress-regular-expressions/
	And:  http://css-tricks.com/snippets/wordpress/get-the-first-image-from-a-post/

USE IN THE LOOP
*/
function extract_tag_from_post($tag){
global $post;
$szPostContent = $post->post_content;
$szSearchPattern = '|<'.$tag.' [^>]*(src="[^"]+")[^>]*|';

preg_match_all( $szSearchPattern, $szPostContent, $aTags );

$iNumberOfTags = count($aTags[0]);
 echo "nuTags" . $iNumberOfTags ;
 $result = "";
 if ( $iNumberOfTags > 0 ) {
 for ( $i=0; $i < $iNumberOfTags ; $i++ ) {
 $result = $result . $aTags[0][$i];
 };
 };
 return $result;
}

Tags: ,