URL Links in [CODE]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Gleek
Forum Newbie
Posts: 1
Joined: Fri Feb 04, 2011 9:23 pm

URL Links in [CODE]

Post by Gleek »

I've been looking for a solution to this problem for days. I basically want clickable (parsed) links within code tags. I understand that quote tags are much more suited for this task, but for what I'm doing it would benefit me greatly if I could use code tags instead.

Here is my current code (this is MyBB):

Code: Select all

* Parses code MyCode.
    *
    * @param string The message to be parsed
    * @param boolean Are we formatting as text?
    * @return string The parsed message.
    */
    function mycode_parse_code($code, $text_only=false)
    {
        global $lang;

        if($text_only == true)
        {
            return "\n{$lang->code}\n--\n{$code}\n--\n";
        }

        // Clean the string before parsing.
        $code = preg_replace('#^(\t*)(\n|\r|\0|\x0B| )*#', '\\1', $code);
        $code = rtrim($code);
        $original = preg_replace('#^\t*#', '', $code);

        if(empty($original))
        {
            return;
        }

        $code = str_replace('$', '$', $code);
        $code = preg_replace('#\$([0-9])#', '\\\$\\1', $code);
        $code = str_replace('\\', '\', $code);
        $code = str_replace("\t", '    ', $code);
        $code = str_replace("  ", '  ', $code);
        $lcurl = 'http://wgtools.com/link-checker/?url=' . urlencode($_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
        $wglc = ' [<a href="' . $lcurl .'" target="_blank"><strong>Check Links</strong></a>]';


        return "<div class=\"codeblock\">\n<div class=\"title\">".$lang->code.$wglc."\n</div><div class=\"body\" dir=\"ltr\"><code>".$code."</code></div></div>\n";
    }

    /**
This is my desired effect:
Image
Post Reply