Unholy Prayer Posted March 10, 2007 Share Posted March 10, 2007 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 More sharing options...
wildteen88 Posted March 10, 2007 Share Posted March 10, 2007 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 More sharing options...
Unholy Prayer Posted March 10, 2007 Author Share Posted March 10, 2007 Ah, thank you very much for your help. Link to comment https://forums.phpfreaks.com/topic/42122-bbcode-help/#findComment-204312 Share on other sites More sharing options...
obsidian Posted March 10, 2007 Share Posted March 10, 2007 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.