Return to Snippet

Revision: 46848
at May 26, 2011 18:24 by kfiil


Updated Code
theme_preprocess_node:

///////////////////////
//Image caption text
//////////////////////
$vars['content'] = _theme_altAsImageText($vars['content']);

template.php:


/**
 * Convert img tags alt text to caption text
 * 
 * @param string $text The text with img tags in
 * 
 * @return string The text with img tags and caption text from the alt field
 */
function _theme_altAsImageText($text)
{
	preg_match_all('/<img[^>]+>/i',$text, $result); 
	$img = array();
	$imgTextResult = array();
	foreach( $result[0] as $key => $img_tag)
	{
	    preg_match_all('/(alt)=("[^"]*")/i',$img_tag, $img[$key]);
	    $imgTextResult[$key] = str_replace('"', "", $img[$key][2][0]);
	}

	foreach( $result[0] as $key => $img_tag)
	{
		if($imgTextResult[$key])
		{
			if(strpos($imgTextResult[$key], 'php print $') === false)
			{			
				$imgText = '
					<div class="picture left">
						<span></span>' . 
						$img_tag
						. '<br />
						' . $imgTextResult[$key] . '
					</div>
				';
				$text =  str_replace($img_tag, $imgText, $text);
			}
		}
	}
	
	return $text;
}

Revision: 46847
at May 26, 2011 18:24 by kfiil


Initial Code
theme_preprocess_node:

///////////////////////
//Image caption text
//////////////////////
$vars['content'] = _fremtidslaboratoriet_altAsImageText($vars['content']);

template.php:


/**
 * Convert img tags alt text to caption text
 * 
 * @param string $text The text with img tags in
 * 
 * @return string The text with img tags and caption text from the alt field
 */
function _theme_altAsImageText($text)
{
	preg_match_all('/<img[^>]+>/i',$text, $result); 
	$img = array();
	$imgTextResult = array();
	foreach( $result[0] as $key => $img_tag)
	{
	    preg_match_all('/(alt)=("[^"]*")/i',$img_tag, $img[$key]);
	    $imgTextResult[$key] = str_replace('"', "", $img[$key][2][0]);
	}

	foreach( $result[0] as $key => $img_tag)
	{
		if($imgTextResult[$key])
		{
			if(strpos($imgTextResult[$key], 'php print $') === false)
			{			
				$imgText = '
					<div class="picture left">
						<span></span>' . 
						$img_tag
						. '<br />
						' . $imgTextResult[$key] . '
					</div>
				';
				$text =  str_replace($img_tag, $imgText, $text);
			}
		}
	}
	
	return $text;
}

Initial URL

                                

Initial Description

                                

Initial Title
Using image ALT text as image caption text

Initial Tags
template, drupal

Initial Language
PHP