Jump to content

Stop other bbcode parsing inside code tags


liamdawe

Recommended Posts

Currently I am using a custom-made simple bbcode parser I need to adjust to not parse anything inside code tags, so these:
    [code]do not parse any bbcode here like [b]this[/b][/code]
 
I want nothing to be parsed inside those.
 
My current code:

    function bbcode($body, $article = 1, $parse_links = 1) 
    { 
     if ($parse_links == 1)
     {
     $URLRegex = '/(??<!(\[\/url\]|\[\/url=))(\s|^))'; // No [url]-tag in front and is start of string, or has whitespace in front
     $URLRegex.= '(';                                    // Start capturing URL
     $URLRegex.= '(https?|ftps?|ircs?):\/\/';            // Protocol
     $URLRegex.= '[\w\d\.\/#\_\-\?:=]+';                        // Any non-space character
     $URLRegex.= ')';                                    // Stop capturing URL
     $URLRegex.= '(??<![.,;!?:\"\'()-])(\/|\[|\s|\.?$))/i';      // Doesn't end with punctuation and is end of string, or has whitespace after
    
     $body = preg_replace($URLRegex,"$2[url=$3]$3[/url]$5", $body);
     
     $find = array(
     "/\[url\=(.+?)\](.+?)\[\/url\]/is",
     "/\[url\](.+?)\[\/url\]/is"
     );
    
     $replace = array(
     "<a href=\"$1\" target=\"_blank\">$2</a>",
     "<a href=\"$1\" target=\"_blank\">$1</a>"
     );
    
     $body = preg_replace($find, $replace, $body);
     }
     
     else if ($parse_links == 0)
     { 
     $find = array(
     "/\[url\=(.+?)\](.+?)\[\/url\]/is",
     "/\[url\](.+?)\[\/url\]/is"
     );
     
     $replace = array(
     "$2",
     "$1"
     );
     
     $body = preg_replace($find, $replace, $body);
     }
    
     // remove the new line after quotes, stop massive spacing
     $body = str_replace("[/quote]\r\n", '[/quote]', $body);
     
     // stop lists having a big gap at the top
     $body = str_replace("[ul]\r\n", '[ul]', $body);
     
     // Quoting an actual person, book or whatever
     $pattern = '/\[quote\=(.+?)\](.+?)\[\/quote\]/is';
     
     $replace = "<blockquote><cite>$1</cite>$2</blockquote>";
     
     while(preg_match($pattern, $body))
     {
     $body = preg_replace($pattern, $replace, $body);
     }
     
     // Quote on its own
     $pattern = '/\[quote\](.+?)\[\/quote\]/is';
     
     $replace = "<blockquote><cite>Quote</cite>$1</blockquote>";
    
     while(preg_match($pattern, $body))
     {
     $body = preg_replace($pattern, $replace, $body);
     }
    
     // replace images and youtube to the correct size if its a comment or forum post (less space!)
     if ($article == 0)
     {
     $find = array(
     "/\[img\]http\:\/\/img\.youtube.com\/vi\/(.+?)\/0\.jpg\[\/img\]/is", //youtube videos done by the old tinymce plugin...
     "/\[img\](.+?)\[\/img\]/is",
     "/\[img=([0-9]+)x([0-9]+)\](.+?)\[\/img\]/is",
     "/\[media=youtube\](.+?)\[\/media\]/is", // This is for videos done by xenforo...
     "/\[youtube\](.+?)\[\/youtube\]/is"
     );
     
     $replace = array(
     "<iframe class=\"youtube-player\" width=\"100%\" height=\"385\" src=\"http://www.youtube.com/embed/$1\" frameborder=\"0\" allowfullscreen>
     </iframe>",
     "<img src=\"$1\" class=\"bbcodeimage-comment img-polaroid\" alt=\"[img]\" />",
     "<img width=\"$1\" height=\"$2\" src=\"$3\" class=\"bbcodeimage-comment img-polaroid\" alt=\"[img]\" />",
     "<iframe class=\"youtube-player\" width=\"550\" height=\"385\" src=\"http://www.youtube.com/embed/$1\" data-youtube-id=\"$1\" frameborder=\"0\" allowfullscreen>
     </iframe>", // for xenforo videos
     "<iframe class=\"youtube-player\" width=\"550\" height=\"385\" src=\"http://www.youtube.com/embed/$1\" data-youtube-id=\"$1\" frameborder=\"0\" allowfullscreen>
     </iframe>"
     );
    
     $body = preg_replace($find, $replace, $body);
     }
     
     //  get rid of empty BBCode, is there a point in having excess markup?
     $body = preg_replace("`\[(b|i|s|u|url|mail|img|quote|code|color|youtube)\]\[/(b|i|s|u|url|mail|img|quote|code|color|youtube)\]`",'',$body);
     
     $find = array(
            "/\[url\=(.+?)\](.+?)\[\/url\]/is",
     "/\[url\](.+?)\[\/url\]/is",
            "/\[b\](.+?)\[\/b\]/is", 
            "/\[i\](.+?)\[\/i\]/is", 
            "/\[u\](.+?)\[\/u\]/is",
            "/\[s\](.+?)\[\/s\]/is",
            "/\[color\=(.+?)\](.+?)\[\/color\]/is",
            "/\[font\=(.+?)\](.+?)\[\/font\]/is",
            "/\[center\](.+?)\[\/center\]/is",
            "/\[right\](.+?)\[\/right\]/is",
            "/\[left\](.+?)\[\/left\]/is",
     "/\[img\]http\:\/\/img\.youtube.com\/vi\/(.+?)\/0\.jpg\[\/img\]/is", //youtube videos done by the old tinymce plugin...
            "/\[img\](.+?)\[\/img\]/is",
            "/\[img=([0-9]+)x([0-9]+)\](.+?)\[\/img\]/is",
            "/\[email\](.+?)\[\/email\]/is",
     "/\[s\](.+?)\[\/s\]/is",
     "/\[media=youtube\](.+?)\[\/media\]/is", // This is for videos done by xenforo...
     "/\[youtube\](.+?)\[\/youtube\]/is",
     '/\[list\](.*?)\[\/list\]/is',
      '/\[\*\](.*?)(\n|\r\n?)/is',
      '/\[ul\]/is',
      '/\[\/ul\]/is',
      '/\[li\]/is',
      '/\[\/li\]/is',
      "/\[size\=(.+?)\](.+?)\[\/size\]/is",
      "/\[email\=(.+?)\](.+?)\[\/email\]/is",
      "/\[justify\](.+?)\[\/justify\]/is",
      "/\[code\](.+?)\[\/code\]/is",
      "/\[sup\](.+?)\[\/sup\]/is"
     );
     
     $replace = array(
            "<a href=\"$1\" target=\"_blank\">$2</a>",
     "<a href=\"$1\" target=\"_blank\">$1</a>",
            "<strong>$1</strong>",
            "<em>$1</em>",
            "<span style=\"text-decoration:underline;\">$1</span>",
            "<del>$1</del>",
            "$2",
            "$2",
            "<div style=\"text-align:center;\">$1</div>",
            "<div style=\"text-align:right;\">$1</div>",
            "<div style=\"text-align:left;\">$1</div>",
     "<iframe class=\"youtube-player\" width=\"100%\" height=\"385\" src=\"http://www.youtube.com/embed/$1\" frameborder=\"0\" allowfullscreen>
    </iframe>",
            "<a class=\"fancybox\" rel=\"group\" href=\"$1\"><img src=\"$1\" class=\"bbcodeimage img-polaroid\" alt=\"[img]\" /></a>",
            "<a class=\"fancybox\" rel=\"group\" href=\"$3\"><img width=\"$1\" height=\"$2\" src=\"$3\" class=\"bbcodeimage img-polaroid\" alt=\"[img]\" /></a>",
            "<a href=\"mailto:$1\" target=\"_blank\">$1</a>",
     "<span style=\"text-decoration: line-through\">$1</span>",
     "<iframe class=\"youtube-player\" width=\"550\" height=\"385\" src=\"http://www.youtube.com/embed/$1\" data-youtube-id=\"$1\" frameborder=\"0\" allowfullscreen>
    </iframe>", // for xenforo videos
     "<iframe class=\"youtube-player\" width=\"550\" height=\"385\" src=\"http://www.youtube.com/embed/$1\" data-youtube-id=\"$1\" frameborder=\"0\" allowfullscreen>
     </iframe>",
     '<ul>$1</ul>',
     '<li>$1</li>',
     '<ul>',
     '</ul>',
     '<li>',
     '</li>',
     '$2',
     '<a href="mailto:$1">$2</a>',
     '$1',
     'Code:<br /><code>$1</code>',
     '<sup>$1</sup>'
     );
     
     $smilies = array( 
     ":><:" => '<img src="/includes/jscripts/sce/emoticons/angry.png" data-sceditor-emoticon=":><:" alt="" />',
     ":><:" => '<img src="/includes/jscripts/sce/emoticons/angry.png" data-sceditor-emoticon=":><:" alt="" />', // for comments as they are made html-safe
     ":'(" => '<img src="/includes/jscripts/sce/emoticons/cry.png" data-sceditor-emoticon=":\'(" alt="" />', // for comments as they are made html-safe
     ":'(" => '<img src="/includes/jscripts/sce/emoticons/cry.png" data-sceditor-emoticon=":'(" alt="" />',
     ":dizzy:" => '<img src="/includes/jscripts/sce/emoticons/dizzy.png" data-sceditor-emoticon=":dizzy:" alt="" />',
     ":D" => '<img src="/includes/jscripts/sce/emoticons/grin.png" data-sceditor-emoticon=":D" alt="" />',
     "^_^" => '<img src="/includes/jscripts/sce/emoticons/happy.png" data-sceditor-emoticon="^_^" alt="" />',
     "<3" => '<img src="/includes/jscripts/sce/emoticons/heart.png" data-sceditor-emoticon="<3" alt="" />',
     "<3" => '<img src="/includes/jscripts/sce/emoticons/heart.png" data-sceditor-emoticon="<3" alt="" />', // for comments as they are made html-safe
     ":huh:" => '<img src="/includes/jscripts/sce/emoticons/huh.png" data-sceditor-emoticon=":huh:" alt="" />',
     ":|" => '<img src="/includes/jscripts/sce/emoticons/pouty.png" data-sceditor-emoticon=":|" alt="" />',
     ":(" => '<img src="/includes/jscripts/sce/emoticons/sad.png" data-sceditor-emoticon=":(" alt=""/>',
     ":O" => '<img src="/includes/jscripts/sce/emoticons/shocked.png" data-sceditor-emoticon=":O" alt="" />',
     ":sick:" => '<img src="/includes/jscripts/sce/emoticons/sick.png" data-sceditor-emoticon=":sick:" alt="" />',
     ":)" => '<img src="/includes/jscripts/sce/emoticons/smile.png" data-sceditor-emoticon=":)" alt="" />',
     ":P" => '<img src="/includes/jscripts/sce/emoticons/tongue.png" data-sceditor-emoticon=":P" alt="" />',
     ":S:" => '<img src="/includes/jscripts/sce/emoticons/unsure.png" data-sceditor-emoticon=":S" alt="" />',
     ":woot:" => '<img src="/includes/jscripts/sce/emoticons/w00t.png" data-sceditor-emoticon=":woot:" alt="" />',
     ":whistle:" => '<img src="/includes/jscripts/sce/emoticons/whistle.png" data-sceditor-emoticon=":whistle:" alt="" />',
     ";)" => '<img src="/includes/jscripts/sce/emoticons/wink.png" data-sceditor-emoticon=";)" alt="" />',
     ":wub:" => '<img src="/includes/jscripts/sce/emoticons/wub.png" data-sceditor-emoticon=":wub:" alt="" />'
     );
    
     $body = str_replace( array_keys( $smilies ), array_values( $smilies ), $body );
    
     $body = preg_replace($find, $replace, $body);
     
     $body = nl2br($body);
     
     // stop there being a big gap between list items
     $body = str_replace('</li><br />', '</li>', $body);
     
     // stop there being a big gap after a list is finished
     $body = str_replace('</ul><br />', '</ul>', $body);
     
     // stop big gaps after embedding a tweet from twitter
     $body = str_replace('</a></blockquote><br />', '</a></blockquote>', $body);
     $body = str_replace('</script><br />', '</script>', $body);
     
     return $body;
    }

Completely stuck on how I can achieve this :(

 

Do the regex for code tags first, replacing anything within these tags into htmlentities. That way the bbcode within these tags will not be parseable.

// replace code tags first
$text = preg_replace_callback("/\[code\](.+?)\[\/code\]/is", 
                                    function($matches)
                                    {
                                        return str_replace(array('[', ']'), array('[', ']'), htmlentities($matches[1]));
                                    },
                              $text);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.