php - correct regex synthax in custom bbcode apllication -
i wish integrated prism synthax highlighter custom built php/mysql cms; having issues regex synthax. wish accomplish:
- allow users post
- text alone,
- or code , text never code alone.
- code may preceded text or text may included after code.
my php code below:
function bbcode2html($var) { // [code] $var = preg_replace('/\[code](.+?)\[\/code]/si', '<section class="language-markup"> <pre><code>$1</code></pre> </section>', $var); return $var; } $var = '[code]<!doctype html>[/code] text'; // verify content input if(!preg_match("/(w+?)|\[code](.+?)\[\/code]/si", $var)) { echo 'the code tags can not empty!'; } elseif(!preg_match("/(w+?)|\[code](.+?)\[\/code](w+)/si", $var)) { echo 'your post contains code, please add text'; }else{ echo $var = bbcode2html(htmlentities($var));}
with present code above, have observed following:
- when text alone posted, feedback 'the code tags can not empty!'
- when text , code posted, feed 'your post contains code, please add text'
i therefore need clues right regex synthax enable me achieve these 2 objectives:
- allow users post text alone, or code , text never code alone.
- code may preceded text or text may included after code.
thanks.
what doing 2 checks:
- verify string contains 1 [[:alnum:]], after stripping
[code]...[/code]
, trimming. - check empty code tags
$var = '[code]<!doctype html>[/code] text'; // code alone or nothing if(!preg_match('~[[:alnum:]]~', trim(preg_replace('~\[code\].*?\[/code\]~', "", $var)))) { echo 'please add text...'; } else { // empty code tags if(preg_match('~\[code\]\s*\[/code\]~', $var)) { echo 'the code tags can not empty!'; // fine } else { echo "wohoooo works!"; } }
Comments
Post a Comment