/halogy/application/helpers/bbcode_helper.php

https://bitbucket.org/haloweb/halogy-1.0/ · PHP · 84 lines · 48 code · 13 blank · 23 comment · 2 complexity · 0d99aefb74e3af67f38f26426a92b491 MD5 · raw file

  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter BBCode Helpers
  4. *
  5. * @package CodeIgniter
  6. * @subpackage Helpers
  7. * @category Helpers
  8. * @author Philip Sturgeon
  9. * @changes MpaK http://mrak7.com
  10. * @link http://codeigniter.com/wiki/BBCode_Helper/
  11. */
  12. // ------------------------------------------------------------------------
  13. /**
  14. * parse_bbcode
  15. *
  16. * Converts BBCode style tags into basic HTML
  17. *
  18. * @access public
  19. * @param string unparsed string
  20. * @param int max image width
  21. * @return string
  22. */
  23. function bbcode($str = '', $max_images = 0)
  24. {
  25. // convert to html entities
  26. $str = htmlentities($str);
  27. $str = auto_link($str);
  28. // Max image size eh? Better shrink that pic!
  29. if($max_images > 0):
  30. $str_max = "style=\"max-width:".$max_images."px; width: [removed]this.width > ".$max_images." ? ".$max_images.": true);\"";
  31. endif;
  32. $find = array(
  33. "'\n'i",
  34. "'\[b\](.*?)\[/b\]'is",
  35. "'\[i\](.*?)\[/i\]'is",
  36. "'\[u\](.*?)\[/u\]'is",
  37. "'\[s\](.*?)\[/s\]'is",
  38. "'\[img\](.*?)\[/img\]'i",
  39. "'\[url\](.*?)\[/url\]'i",
  40. "'\[url=(.*?)\](.*?)\[/url\]'i",
  41. "'\[link\](.*?)\[/link\]'i",
  42. "'\[link=(.*?)\](.*?)\[/link\]'i",
  43. "'\[size=small\](.*?)\[/size\]'is",
  44. "'\[size=normal\](.*?)\[/size\]'is",
  45. "'\[size=medium\](.*?)\[/size\]'is",
  46. "'\[size=big\](.*?)\[/size\]'is",
  47. "'\[quote\](.*?)\[/quote\]'is",
  48. "'\[code\](.*?)\[/code\]'is"
  49. );
  50. $replace = array(
  51. '<br />',
  52. '<strong>\\1</strong>',
  53. '<em>\\1</em>',
  54. '<u>\\1</u>',
  55. '<s>\\1</s>',
  56. '<img src="\\1" alt="" />',
  57. '<a href="\\1">\\1</a>',
  58. '<a href="\\1">\\2</a>',
  59. '<a href="\\1">\\1</a>',
  60. '<a href="\\1">\\2</a>',
  61. '<span style="font-size:0.9em;">\\1</span>',
  62. '<span style="font-size:1em;">\\1</span>',
  63. '<span style="font-size:1.2em;">\\1</span>',
  64. '<span style="font-size:1.4em;">\\1</span>',
  65. '</p><blockquote>\\1</blockquote><p>',
  66. '<pre><code>\\1</pre></code>'
  67. );
  68. $str = preg_replace($find, $replace, $str);
  69. return '<p>'.$str.'</p>';
  70. }
  71. ?>