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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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