smerny Posted July 23, 2009 Share Posted July 23, 2009 What is the best way to convert [ b][ i] etc to html? I would like it ONLY to convert if there is a matching end tag, so as not to have the rest of the page also edited... so... "[ b]blahblah[ /b] [ b]blah" would be converted to "<b>blahblah</b> [ b]blah" Quote Link to comment https://forums.phpfreaks.com/topic/167154-solved-converting-b-to-html/ Share on other sites More sharing options...
conker87 Posted July 23, 2009 Share Posted July 23, 2009 You could use str_replace: str_replace("[b]", "<b>", $string); Hmm, after reading the end of your post, this wouldn't be waht you're looking for. Just disregard it. Quote Link to comment https://forums.phpfreaks.com/topic/167154-solved-converting-b-to-html/#findComment-881358 Share on other sites More sharing options...
wildteen88 Posted July 23, 2009 Share Posted July 23, 2009 Your best bet is regex if you want the tags to match. $str = '[i]Hello [b]World[/b][/i] [u]Underline[/u]'; $str = preg_replace('~\[b\](.*)\[/b\]~', '<b>$1</b>', $str); $str = preg_replace('~\[i\](.*)\[/i\]~', '<i>$1</i>', $str); $str = preg_replace('~\[u\](.*)\[/u\]~', '<u>$1</u>', $str); Quote Link to comment https://forums.phpfreaks.com/topic/167154-solved-converting-b-to-html/#findComment-881359 Share on other sites More sharing options...
Adam Posted July 23, 2009 Share Posted July 23, 2009 I'd use a non-greedy match: (.*?) Quote Link to comment https://forums.phpfreaks.com/topic/167154-solved-converting-b-to-html/#findComment-881367 Share on other sites More sharing options...
smerny Posted July 23, 2009 Author Share Posted July 23, 2009 i really need to learn this preg stuff... anyone suggest a good tutorial? Quote Link to comment https://forums.phpfreaks.com/topic/167154-solved-converting-b-to-html/#findComment-881453 Share on other sites More sharing options...
Adam Posted July 23, 2009 Share Posted July 23, 2009 You need to learn regular expressions; specifically Perl compatible regular expressions if you want to use them with the 'preg' functions. There's a 'resources' sticky in the regex forum. Quote Link to comment https://forums.phpfreaks.com/topic/167154-solved-converting-b-to-html/#findComment-881474 Share on other sites More sharing options...
ldougherty Posted July 23, 2009 Share Posted July 23, 2009 Check out http://www.regular-expressions.info/ This is a good resource site for regex Quote Link to comment https://forums.phpfreaks.com/topic/167154-solved-converting-b-to-html/#findComment-881476 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.