So I have created this function in PHP to output text in the required form. It is a simple BB-Code system. I have cut out the other BB-Codes from it to keep it shorter (Around 15 cut out)
My issue is the final one [title=blue]Test[/title] (Test data) does not work. It outputs exactly the same. I have tried 4-5 different versions of the REGEX code and nothing has changed it.
Does anyone know where I am going wrong or how to fix it?
function bbcode_format($str){
$str = htmlentities($str);
$format_search = array(
'#\[b\](.*?)\[/b\]#is',
'#\[title=(.*?)\](.*?)\[/title\]#i'
);
$format_replace = array(
'<strong>$1</strong>',
'<div class="box_header" id="$1"><center>$2</center></div>'
);
$str = preg_replace($format_search, $format_replace, $str);
$str = nl2br($str);
return $str;
}
Change the delimiter
#to/. And change “/[/b\]” to “\[\/b\]“. You need to escape the “/” since you need it as literal character.Maybe the “
array()” should use brackets: “array[]“.Note: I borrowed the answer from here: Convert BBcode to HTML using JavaScript/jQuery
Edit: I forgot that “/” isn’t a metacharacter so I edited the answer accordingly.
Update: I wasn’t able to make it work with function, but this one works. See the comments. (I used the fiddle on the accepted answer for testing from the question I linked above. You may do so also.) Please note that this is JavaScript. You had PHP code in your question. (I can’t help you with PHP code at least for awhile.)
Update2: Now with PHP… (Sorry, you have to format the
$replacementsto your liking. I just added some tags and text to demostrate the changes.) If there’s still trouble with the “title”, see what kind of text you are trying to format. I made the title “=” optional with?so it should work properly work texts like: “[title=id with one or more words]Title with id[/title]” and “[title]Title without id[/title]. Not sure thought if theidattribute is allowed to have spaces, I guess not: http://reference.sitepoint.com/html/core-attributes/id.