Using image ALT text as image caption text


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. theme_preprocess_node:
  2.  
  3. ///////////////////////
  4. //Image caption text
  5. //////////////////////
  6. $vars['content'] = _theme_altAsImageText($vars['content']);
  7.  
  8. template.php:
  9.  
  10.  
  11. /**
  12.  * Convert img tags alt text to caption text
  13.  *
  14.  * @param string $text The text with img tags in
  15.  *
  16.  * @return string The text with img tags and caption text from the alt field
  17.  */
  18. function _theme_altAsImageText($text)
  19. {
  20. preg_match_all('/<img[^>]+>/i',$text, $result);
  21. $img = array();
  22. $imgTextResult = array();
  23. foreach( $result[0] as $key => $img_tag)
  24. {
  25. preg_match_all('/(alt)=("[^"]*")/i',$img_tag, $img[$key]);
  26. $imgTextResult[$key] = str_replace('"', "", $img[$key][2][0]);
  27. }
  28.  
  29. foreach( $result[0] as $key => $img_tag)
  30. {
  31. if($imgTextResult[$key])
  32. {
  33. if(strpos($imgTextResult[$key], 'php print $') === false)
  34. {
  35. $imgText = '
  36. <div class="picture left">
  37. <span></span>' .
  38. $img_tag
  39. . '<br />
  40. ' . $imgTextResult[$key] . '
  41. </div>
  42. ';
  43. $text = str_replace($img_tag, $imgText, $text);
  44. }
  45. }
  46. }
  47.  
  48. return $text;
  49. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.