Jump to content

regex synthax in prism synthax highlighter bbcode application


terungwa

Recommended Posts

I am building a bbcode system to integrate the prism synthax highlighter into my custom php bulletin board; there are some issues with my regex synthax. This is what i need to achieve:

  1. Allow users to post
    • text alone,
    • or code and text but never code alone.
  2. Code may be preceded by text or text may be included after the code.

bbcode

// Function to convert BBcode in HTML tags
	function formatBBcode($str)
	{
	  $str = preg_replace('/\[code](.+?)\[\/code]/si',
	 '<section class="language-php"><pre><code>$1</code></pre></section>', $str);
	  return $str;
	}

validation code:

$var = '[code]<!DOCTYPE html>[/code] Some text';

// post text alone or nothing at all
if(!preg_match('~[[:alnum:]]~', trim(preg_replace('~\[code\].*?\[/code\]~', "", $var))))
{
    echo 'Please add some text...';
} else {

    // empty code tags
    if(preg_match('~\[code\]\s*\[/code\]~', $var))
    {
        echo 'The code tags can not be empty!';

    // If there are no issues
    } else {
        echo "Everything is ok!";
    }
}

With the present code, this is my observation when code alone is posted:

  • If the code is on a single line the 'Please add some text' warning is thrown up.

          e.g

//This throws up warning 'please add some text'
[code]some code[/code]
  • However if the code is on multiple lines, then it gets posted without any warning.

        e.g

//This does not throw up any warning but gets posted
[code]some code
[/code]

I need help in resolving this.

 

Thanks.

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.