php_joe Posted March 3, 2007 Share Posted March 3, 2007 I'm editing a bbs program I have and I'm having problems changing the BBCode into HTML. I tried: $message = str_replace('[b]', '<b>', $message); which didn't work. I tried: $message = str_replace('[', '<', $message); which also didn't work. Why can't I change '['? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted March 3, 2007 Share Posted March 3, 2007 I think you have it backwards Change this: $message = str_replace('[b]', '<b>', $message); To $message = str_replace('<b>', '[b]', $message); <a href="http://us2.php.net/str_replace">str_replace</a> EDIT: If that doesn't work, try this. $message = preg_replace('[b]','<b>', $message); Quote Link to comment Share on other sites More sharing options...
php_joe Posted March 3, 2007 Author Share Posted March 3, 2007 Actually, I'm trying to change [b] into <b> But, for some reason, when I tried $clean_message = str_replace('[b]', '<b>', $message); by itself it works, but when I put $clean_message = str_replace('[b]', '<b>', $message); $clean_message = str_replace('[/b]', '</b>', $message); In, only the second one works. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted March 3, 2007 Share Posted March 3, 2007 Try giving your variable different names? And don't you mean to put $clean_message in the second one? If you don't you will get two strings, one stripping the and leaving the and vise versa. $clean = str_replace('[b]', '<b>', $message); $clean_message = str_replace('[/b]', '</b>', $clean); Try that. I am just kinda playing around with it...if this doesn't work I will actually go into my FTP and try working with it. Did you try the preg_replace() function I suggested in my last post? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 3, 2007 Share Posted March 3, 2007 Use arrays instead of multiple instances of str_replace, example: <?php $msg = "Hello [b]world[/b]! [u][i]Ohhhh[/i][/u]!"; $bbcode = array( '[b]', '[/b]', '[i]', '[/i]', '[u]', '[/u]' ); $html = array( '<b>', '</b>', '<i>', '</i>', '<u>', '</u>' ); $msg = str_replace($bbcode, $html, $msg); echo $msg; ?> However whne doing bbcodes I prefer to use regex. regex is more powerful allowing you to do more advanced bbcodes Quote Link to comment Share on other sites More sharing options...
php_joe Posted March 3, 2007 Author Share Posted March 3, 2007 Thanks guys, using multiple variable names works. I have not tried the preg_replace or array yet, but I will. Joe Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted March 3, 2007 Share Posted March 3, 2007 I would definitely go with wildteen88 advice if you have multiple tags you are trying to do it with. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted March 3, 2007 Share Posted March 3, 2007 I would definitely go with wildteen88 advice if you have multiple tags you are trying to do it with. if you have multiple tags, just add single str_replaces, and: I think you have it backwards Change this: $message = str_replace('[b]', '<b>', $message); To $message = str_replace('<b>', '[b]', $message); <a href="http://us2.php.net/str_replace">str_replace</a> EDIT: If that doesn't work, try this. $message = preg_replace('[b]','<b>', $message); This: $message = str_replace('[b]', '<b>', $message); is actually correct. Ted Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted March 3, 2007 Share Posted March 3, 2007 ted_chou12 - Would it not be easier to use the array and replace every single tag in one go? And yeah, I figured that I was the one who actually had it backwards soon after I posted that xP Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted March 3, 2007 Share Posted March 3, 2007 yeah, array does work here, however, special tags such as link would require the work done separately. Ted Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 3, 2007 Share Posted March 3, 2007 yeah, array does work here, however, special tags such as link would require the work done separately. Ted That's why I mentioned to do regex with advanced tags such as link-title str_replace is just too basic. You can do it on swell swop with regex. However reqex does take time to get used to. It tool me a few months to get used to it. Heres a code snippet for bbcode parser with regex to get you started quickly - CLICKY Quote Link to comment Share on other sites More sharing options...
php_joe Posted March 3, 2007 Author Share Posted March 3, 2007 I've been messing with this all day. I think that there must be some kind of conflict with the rest of the code, because it's just not behaving right. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted March 3, 2007 Share Posted March 3, 2007 yeah, array does work here, however, special tags such as link would require the work done separately. Ted That's why I mentioned to do regex with advanced tags such as link-title str_replace is just too basic. You can do it on swell swop with regex. However reqex does take time to get used to. It tool me a few months to get used to it. Heres a code snippet for bbcode parser with regex to get you started quickly - CLICKY wildteen, does this work? because I am having some trouble with bb codes, maybe i can use this. Ted Quote Link to comment Share on other sites More sharing options...
php_joe Posted March 3, 2007 Author Share Posted March 3, 2007 Heres a code snippet for bbcode parser with regex to get you started quickly - CLICKY Thank you very much for this function. I hope I can get it to work! Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 3, 2007 Share Posted March 3, 2007 To get it to work you just call the function bbcode_format and pass the string with your bbcodes in as the first parameter, example: [code=php:0]<?php // place bbcode parser code snippet here $msg = "[b]Hello world![/b] Umm [u]BBCODES[/u] [url=http://www.google.com]google.com[/url] <-- Ohh! a link"; $msg = bbcode_format($msg); echo nl2br($msg); ?> Quote Link to comment Share on other sites More sharing options...
php_joe Posted March 5, 2007 Author Share Posted March 5, 2007 The function worked great, thank you very much! I was able to cut out the second function by adding to the first one. I also added >>> <s> to the array. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.