Jump to content

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.

Edited by terungwa
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.