SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/BbCode.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16. class BbCode
  17. {
  18.  
  19. /**
  20.   * Convert an inline run of plain text into HTML.
  21.   *
  22.   * Supports the following bbcode tags:
  23.   * [b] [i] [u] [s] [code] [url=...]
  24.   *
  25.   * @param string $text Plain text to convert
  26.   * @param array $tags Optional array of tags names (e.g. 'b') to process. Provide NULL for defaults (b, i, code, url)
  27.   * @return string HTML
  28.   **/
  29. public static function inline($text, $tags = null)
  30. {
  31. $text = Enc::cleanfunky($text);
  32. $text = Enc::html($text);
  33.  
  34. if ($tags === null) {
  35. $tags = array('b', 'i', 'code', 'url');
  36. }
  37.  
  38. if (in_array('b', $tags)) $text = preg_replace('/\[b\](.+?)\[\/b\]/', '<b>$1</b>', $text);
  39. if (in_array('i', $tags)) $text = preg_replace('/\[i\](.+?)\[\/i\]/', '<i>$1</i>', $text);
  40. if (in_array('u', $tags)) $text = preg_replace('/\[u\](.+?)\[\/u\]/', '<u>$1</u>', $text);
  41. if (in_array('s', $tags)) $text = preg_replace('/\[s\](.+?)\[\/s\]/', '<s>$1</s>', $text);
  42. if (in_array('code', $tags)) $text = preg_replace('/\[code\](.+?)\[\/code\]/', '<code>$1</code>', $text);
  43. if (in_array('url', $tags)) $text = preg_replace('/\[url=(.+?)\](.+?)\[\/url\]/', '<a href="$1">$2</a>', $text);
  44.  
  45. return $text;
  46. }
  47.  
  48.  
  49. /**
  50.   * Convert multi-line text into HTML, with support for bbcode.
  51.   * The output will be surrounded by one or more P tags as appropriate
  52.   *
  53.   * @param string $text Plain text to convert
  54.   * @param array $tags Optional array of tags names (e.g. 'b') to process. Provide NULL for defaults (b, i, code, url)
  55.   * @return string HTML
  56.   **/
  57. public static function block($text, $tags = null)
  58. {
  59. $text = self::inline($text, $tags);
  60. $text = Text::widont($text);
  61. $text = Text::autoP($text);
  62. }
  63.  
  64. }
  65.