terungwa Posted May 23, 2014 Share Posted May 23, 2014 (edited) 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: Allow users to post text alone, or code and text but never code alone. 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 May 23, 2014 by terungwa Quote Link to comment https://forums.phpfreaks.com/topic/288717-regex-synthax-in-prism-synthax-highlighter-bbcode-application/ Share on other sites More sharing options...
requinix Posted May 23, 2014 Share Posted May 23, 2014 Your formatBBcode() uses the /s flag in its regular expression but you don't use it again when checking for text. Without it the .*? won't match the newlines. Quote Link to comment https://forums.phpfreaks.com/topic/288717-regex-synthax-in-prism-synthax-highlighter-bbcode-application/#findComment-1480634 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.