Jump to content

bbcode help


Unholy Prayer

Recommended Posts

This is my first time doing bbcode so I'm not really sure how to do this.  I copied this from a tutorial but it doesn't work.

 

$string = "I am so [b]cool[/b] ";
$bb-replace = array('[b]','[/b]','[i]',[/i]');
$bb-replacements = array ('<b>','</b>','</i>','</i>');
$string = str_replace($bb-replace,$bb-replacements,$string);

 

The error is a syntax error unexpected T_ARRAY.  Thanks in advance.

 

 

Link to comment
https://forums.phpfreaks.com/topic/42122-bbcode-help/
Share on other sites

There is a missing quote for last item in the bb-replace array. This is the correct code:

$bb-replace = array('[b]','[/b]','[i]','[/i]');

 

Also note that you cannot use hyphens in array names. You can only use letters, numbers and underscores for variable names. No other characters are allowed.

 

Change the hyphens (-) to underscores in stead.

 

So the correct code:

$string = "I am so [b]cool[/b] ";

$bb_replace = array('[b]', '[/b]', '[i]', '[/i]');
$bb_replacements = array ('<b>', '</b>', '</i>', '</i>');

$string = str_replace($bb_replace, $bb_replacements, $string);

echo $string;

Link to comment
https://forums.phpfreaks.com/topic/42122-bbcode-help/#findComment-204309
Share on other sites

Just a side note, you'll be much safer with bbcode parsing to use regular expression matching. The way you have it now, every opening [ b ] tag will be replaced with an opening bold whether or not it has a closing tag. If you'll go with a regexp match, you can match only complete tag sets:

<?php
$string = "I am so [b]cool[/b]!";
$bb_tags = array('|\[([bi])\]([^[]+)\[/\\1\]|i');
$bb_replace = array('<\1>\2</\1>');
$string = preg_replace($bb_tags, $bb_replace, $string);
?>

 

Good luck.

Link to comment
https://forums.phpfreaks.com/topic/42122-bbcode-help/#findComment-204322
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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