DeanWhitehouse Posted July 18, 2008 Share Posted July 18, 2008 can someone show me the basic preg-replace for bbcode. so when they enter <center> or it centers the text. Thanks, Blade Quote Link to comment https://forums.phpfreaks.com/topic/115430-solved-basic-preg-replace-for-bbcode/ Share on other sites More sharing options...
JasonLewis Posted July 18, 2008 Share Posted July 18, 2008 Lol, you centered the text. Like this: $str = " [center][b]Bold and centered text![/b][/center] "; $str = preg_replace("/\[center\](.*?)\[\/center\]/","<center>\\1</center>",$str); $str = preg_replace("/\[b\](.*?)\[\/b\]/","<b>\\1</b>",$str); echo $str; Here's a link to basic regular expression help in PHP: http://www.sitepoint.com/article/regular-expressions-php Quote Link to comment https://forums.phpfreaks.com/topic/115430-solved-basic-preg-replace-for-bbcode/#findComment-593405 Share on other sites More sharing options...
DeanWhitehouse Posted July 18, 2008 Author Share Posted July 18, 2008 ok, thanks for that , now how can i search to see if the string contains any html code. e.g <center> ? or do i not nead to , i want it so that if they enter <center> or [center ] it centers the text Quote Link to comment https://forums.phpfreaks.com/topic/115430-solved-basic-preg-replace-for-bbcode/#findComment-593431 Share on other sites More sharing options...
JasonLewis Posted July 18, 2008 Share Posted July 18, 2008 Um, well you wouldn't really want them to use HTML code, what exactly is it for? Quote Link to comment https://forums.phpfreaks.com/topic/115430-solved-basic-preg-replace-for-bbcode/#findComment-593433 Share on other sites More sharing options...
DeanWhitehouse Posted July 18, 2008 Author Share Posted July 18, 2008 forum, bbcode , i am planning on limiting what html they can use, to <center>,<b> etc. but i don't know how to search the string for it , strbck is the closet function i have seen Quote Link to comment https://forums.phpfreaks.com/topic/115430-solved-basic-preg-replace-for-bbcode/#findComment-593442 Share on other sites More sharing options...
JasonLewis Posted July 18, 2008 Share Posted July 18, 2008 You could just replace the [ and ] in the center preg_replace with < and > Code: $str = preg_replace("/\<center\>(.*?)\<\/center\>/","<center>\\1</center>",$str); Quote Link to comment https://forums.phpfreaks.com/topic/115430-solved-basic-preg-replace-for-bbcode/#findComment-593450 Share on other sites More sharing options...
DeanWhitehouse Posted July 18, 2008 Author Share Posted July 18, 2008 ok, last problem, i want to use this on lots of pages , from a forum, to members profiles. how would i do this without writing it on every page , i was think something like function bbcode(code) { $str = preg_replace("/\[center\](.*?)\[\/center\]/","<center>\\1</center>",code); $str = preg_replace("/\[b\](.*?)\[\/b\]/","<b>\\1</b>",code); } then to call, i would do bbcode(cake); ?? is that correct Quote Link to comment https://forums.phpfreaks.com/topic/115430-solved-basic-preg-replace-for-bbcode/#findComment-593476 Share on other sites More sharing options...
DeanWhitehouse Posted July 18, 2008 Author Share Posted July 18, 2008 Is this how i call from a function? <?php function bbcode(code) { $str = preg_replace("/\[center\](.*?)\[\/center\]/","<center>\\1</center>",$str); $str = preg_replace("/\[b\](.*?)\[\/b\]/","<b>\\1</b>",$str); $str = preg_replace("/\<center\>(.*?)\<\/center\>/","<center>\\1</center>",$str); } echo bbcode([b]cake[/b]) ?> Quote Link to comment https://forums.phpfreaks.com/topic/115430-solved-basic-preg-replace-for-bbcode/#findComment-593535 Share on other sites More sharing options...
JasonLewis Posted July 19, 2008 Share Posted July 19, 2008 I would use a return inside the function. <?php function bbcode(code) { $str = preg_replace("/\[center\](.*?)\[\/center\]/","<center>\\1</center>",$str); $str = preg_replace("/\[b\](.*?)\[\/b\]/","<b>\\1</b>",$str); $str = preg_replace("/\<center\>(.*?)\<\/center\>/","<center>\\1</center>",$str); return $str; //return the formatted string } $toformat = " [center]Hello There! [b]How are you today?[/b][/center] "; $toecho = bbcode($toformat); echo $toecho; ?> Quote Link to comment https://forums.phpfreaks.com/topic/115430-solved-basic-preg-replace-for-bbcode/#findComment-593953 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.