1

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;
}
hakre
  • 193,403
  • 52
  • 435
  • 836
Rynosapien
  • 23
  • 1
  • 4

1 Answers1

3

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.)

$str = 'this is a [b]bolded[/b], [title=xyz xyz]Title of something[/title]';

//doesn't work (PHP function)
//$str = htmlentities($str);

//notes: lose the single quotes
//lose the text "array" and use brackets
//don't know what "ig" means but doesn't work without them
$format_search =  [
/\[b\](.*?)\[\/b\]/ig,
/\[title=(.*?)\](.*?)\[\/title\]/ig
];

$format_replace = [
  '<strong>$1</strong>',
  '<div class="box_header" id="$1"><center>$2</center></div>'
];

// Perform the actual conversion
for (var i =0;i<$format_search.length;i++) {
  $str = $str.replace($format_search[i], $format_replace[i]);
}

//place the formatted string somewhere
document.getElementById('output_area').innerHTML=$str;

Update2: Now with PHP... (Sorry, you have to format the $replacements to 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 the id attribute is allowed to have spaces, I guess not: http://reference.sitepoint.com/html/core-attributes/id.

$str = '[title=title id]Title text[/title] No style, [b]Bold[/b], [i]emphasis[/i], no style.';

//try without this if there's trouble
$str = htmlentities($str);

//"#" works as delimiter in PHP (not sure abut JS) so no need to escape the "/" with a "\"
$patterns = array();
$patterns = array(
  '#\[b\](.*?)\[/b\]#',
  '#\[i\](.*?)\[/i\]#', //delete this row if you don't neet emphasis style
  '#\[title=?(.*?)\](.*?)\[/title\]#'
);

$replacements = array();
$replacements = array(
  '<strong>$1</strong>',
  '<em>$1</em>', // delete this row if you don't need emphasis style
  '<h1 id="$1">$2</h1>'
);

//perform the conversion
$str = preg_replace($patterns, $replacements, $str);
echo $str;
Community
  • 1
  • 1
ZZ-bb
  • 2,157
  • 1
  • 24
  • 33
  • I edited the code to not use # and made any / become \/ But the [title=][/title] still won't work? – Rynosapien Apr 09 '12 at 19:38
  • @Rynosapien See my updated answer. I was able to make it work somewhat fine. – ZZ-bb Apr 09 '12 at 20:31
  • @Rynosapien Whether the regex works or not depends on the structure of your string to be formatted. [title=xxx yyy]zzz yyy[/title] should now accept multiple words as title id and title text. – ZZ-bb Apr 09 '12 at 20:37
  • I tried the RegEx you provided and nothing has changed, I think it might just be the PHP restricting it? I have done exactly as you have said and it still doesn't work! – Rynosapien Apr 09 '12 at 21:29
  • @Rynosapien Updated my answer with PHP code. If it doesn't work your PHP engine is really old. I believe this should work even with PHP 4.2.0. Check with `phpinfo()` if you have "PCRE (Perl Compatible Regular Expressions) Support" enabled. – ZZ-bb Apr 10 '12 at 07:31
  • I changed my code, yet again, to match yours. This time it is perfect. Works every time! Thank you for all your help, you have really gone out of your way to fix this for me! I can't thank you enough! – Rynosapien Apr 10 '12 at 09:31