view bbcodeparser.inc.php @ 18:98983ba58e2e default tip

Added tag owls-2.0 for changeset 081ba8764994
author markus schnalke <meillo@marmaro.de>
date Sat, 23 Jul 2016 21:39:40 +0200 (2016-07-23)
parents 081ba8764994
children
line wrap: on
line source
<?php
/**
 * BB-Code-Parser
 *
 * @author Meillo  r e t u r n s <meillo@marmaro.de>
 */


  /// path to smilies
  define('SMILIE_DIR', 'smilies/');



  /**
   * parses recursive quotes without a source mentioned
   *
   * @param $textinput the bbcode-text
   * @param $level number of levels to go inside
   * @return HTML-text
   */
  function parse_quote1($textinput,$level = 1) {
    $pattern = '#\[quote\](((?R)|(.*))*)\[/quote\]#isUe';
    $replacement = "'<br />[nl]<span class=\"quote0\">Zitat:</span>[nl]<div class=\"quote1\">[nl]'.parse_quote1('$1',
                     ". ($level + 1) ."
                   ).'[nl]</div>[nl]'";
    return preg_replace($pattern, $replacement, $textinput);
  }

  /**
   * parses recursive quotes with a source mentioned
   *
   * @param $textinput the bbcode-text
   * @param $level number of levels to go inside
   * @return HTML-text
   */
  function parse_quote2($textinput,$level = 1) {
    $pattern = '#\[quote\=(.*)\](((?R)|(.*))*)\[/quote\]#isUe';
    $replacement = "'<br />[nl]<span class=\"quote0\">Zitat: ($1)</span>[nl]<div class=\"quote2\">[nl]'.parse_quote2('$2',
                     ". ($level + 1) ."
                   ).'[nl]</div>[nl]'";
    return preg_replace($pattern, $replacement, $textinput);
  }

  /**
   * replaces smilies
   *
   * @param $text text with ASCII-smilies
   * @return text with [img]-smilies
   */
  function smilies($text) {
    $smilies = array( ':-?\)'        => 'Smile.gif',
                      ';-?\)'        => 'Wink.gif',
                      ':-?D'        => 'Biggrin.gif',
                      ':-?\('        => 'Sad.gif',
                      ':-?P'        => 'Lick.gif',
                      ':o'         => 'Talk.gif',
                      ':-S'        => 'Dontknow.gif',
                      ':dontknow:' => 'Dontknow.gif',
                      ':-@'        => 'Angry.gif',
                      ':cool:'     => 'Cool.gif',
                      'B-\)'        => 'Cool.gif',
                      '%-\)'        => 'Crosseyed.gif',
                      '%-\('        => 'Crosseyed.gif',
                      ':rolleyes:' => 'Rolleyes.gif',
                      ':eek:'      => 'Shocked.gif');
    while(list($key, $val) = each($smilies)) {
	$key = '/(\s|^)'.$key.'(\s|$)/';
	$text = preg_replace($key, '$1[img]'. SMILIE_DIR . $val .'[/img]$2', $text);
    }
    return $text;
  }


  /**
   * turns bbcode in HTML
   *
   * @param $text the text with bbcode inside
   * @param $smilies set to 1 causes smilies to be replaced with pics
   * @param $images set to 1 causes images to be displayed ([img]-tag)
   * @return text with HTML-code
   */
  function bbcode($text, $smilies = 0, $images = 0) {

    // line breaks and special chars
    $text = preg_replace("#(\r\n)|(\r)#", "\n", htmlentities($text, ENT_COMPAT, 'UTF-8'));
    $text = preg_replace("#(\n){3,}#", "\n\n", $text);

    // smilies
    if ($smilies == 1) {
      $text = smilies($text);
    }

		/*
    // new-lines
    $text = preg_replace("#(\r\n)|(\r)#", "\n", $text);
    $text = str_replace("\n", '<br />[nl]', htmlentities($text, ENT_COMPAT, 'UTF-8'));

    // bold
    $text = preg_replace("#\[b\](.*?)\[/b\]#i", "<strong>$1</strong>", $text);
    // italic
    $text = preg_replace("#\[i\](.*?)\[/i\]#i", "<i>$1</i>", $text);
    // links
    $text = preg_replace("#\[url\](.*)\[/url\]#iU", "<a href=\"$1\">$1</a>", $text);
    $text = preg_replace("#\[url=(.*)\](.*)\[/url\]#iU", "<a href=\"$1\">$2</a>", $text);
    // lists
      //$text = preg_replace("#\[list\]\<br /\>(.*)\[/list\]#iU", "<ul>$1</ul>", $text);
      //$text = preg_replace("#\[\*\](.*)\<br \/\>#iU", "<li>$1</li>", $text);
    // quotes
		*/
    $text = parse_quote1($text);
    $text = parse_quote2($text);
    // images
    if ($images == 1) {
      $text = preg_replace("#\[img\](.*?)\[/img\]#i", "<img src=\"$1\" alt=\"&lt;[Bild]&gt;\" />", $text);
    }

		/*
    // remove backslashes
    $text = preg_replace("#\\\#is", "", $text);
    // new-lines
    $text = str_replace('[nl]', "\n", $text);
		*/


    // inline
    $text = preg_replace('#\[b\](.*)\[/b\]#iU', '<strong>$1</strong>', $text);
    $text = preg_replace('#\*(.*)\*#iU', '<strong>$1</strong>', $text);
    $text = preg_replace('#\[i\](.*)\[/i\]#iU', '<i>$1</i>', $text);
    $text = preg_replace('#[^A-Za-z0-9]_(.*)_[^A-Za-z0-9]#iU', '<em>$1</em>', $text);
    $text = preg_replace('#{{{(.*)}}}#iUs', '<tt>$1</tt>', $text);
    $text = preg_replace("#\[url\](.*)\[/url\]#iU","<a href=\"$1\">$1</a>",$text);
    $text = preg_replace("#\[url=(.*)\](.*)\[/url\]#iU","<a href=\"$1\">$2</a>",$text);
    $text = preg_replace("#\[img\](.*)\[/img\]#iU","<img src=\"$1\" alt=\"[Bild]\" />", $text);
    $text = preg_replace("#^-{3,}$#iUm","<hr />", $text);

    // Listen
#      $text = preg_replace("#\[list\](.*)\[/list\]#i","<ul>$1</ul>", $text);
    $text = preg_replace("#^\s*\*(.*)\n#imU", "<li>$1</li>", $text);
    $text = preg_replace("#^((\s*\<li\>(.*)\</li\>\s*)+)$#im", "<ul>$1</ul>", $text);




    // boxes


    $text = preg_replace('#^(.+)$#mU', "<p>\n  $1\n</p>", $text);

		# nicht wenn <h_> oder <ul> enthalten sind
    $text = preg_replace("#\<p\>\n  \[h([1-6]{1})\](.*)\[/h[1-6]{1}\]\n\</p\>#iU", "\n<h$1>$2</h$1>", $text);
    $text = preg_replace("#\<p\>\n  ===(.*)\n\</p\>#iU", "\n<h3>$1</h3>", $text);
    $text = preg_replace("#\<p\>\n  ==(.*)\n\</p\>#iU", "\n<h2>$1</h2>", $text);

    $text = preg_replace("#\<p\>\n  \<ul\>(.*)\</ul\>\n\</p\>#iU", "<ul>\n  $1\n</ul>", $text);
    return $text;
  }

?>