Gordon Werner Posted May 4, 2010 Share Posted May 4, 2010 Good morning. If I want a user to be able to enter simple markup into a text field using [tag]xyz[/tag] format so that I can convert it on display to <tag>xyz</tag> format, how do I formulate the preg_replace expressions? i.e. [b]some text[/b] [i]some text[/i] [u]some text[/u] [url]http://www.www.com[/url] [email][email protected][/email] [img=http://www.com/image.jpg] etc ... should become: <b>some text</b> <i>some text</i> <u>some text</u> <a href="http://www.www.com">http://www.www.com</a> <a href="mailto:[email protected]">[email protected]</a> <img src="http://www.com/image.jpg" /> etc ... also if I want the user to be able to enter the following: [tm] [sm] [r] [c] and have the display change those tags to the ascii characters ... would str_replace be the best way to handle those? or would preg_replace also work? Any help would be appreciated as I am trying to learn this stuff and haven't found any good examples that are clear to a beginner. Thanks G. Link to comment https://forums.phpfreaks.com/topic/200700-preg_replace-question/ Share on other sites More sharing options...
Ken2k7 Posted May 4, 2010 Share Posted May 4, 2010 preg_replace will work. It can take in arrays as arguments so you can just put up 2 arrays. First array is the regexp and the second array is the replacement. So for example: $bb = array( '#\[b\](.+?)\[/b\]#i', '#\[i\](.+?)\[/i\]#i', ); $replacement = array( '<b>$1</b>', '<i>$1</i>', ); $new_str = preg_replace($bb, $replacement, $str); Link to comment https://forums.phpfreaks.com/topic/200700-preg_replace-question/#findComment-1053190 Share on other sites More sharing options...
Psycho Posted May 4, 2010 Share Posted May 4, 2010 Here's something I have laying around <?php //bbcode $patterns = array( //BB Code "/\[[b]\](.*?)\[\/b\]/is", "/\[[i]\](.*?)\[\/i\]/is", "/\[[u]\](.*?)\[\/u\]/is", "/\[[s]\](.*?)\[\/s\]/is", "/\[marquee\](.*?)\[\/marquee\]/is", "/\[url\](.*?)\[\/url\]/is", "/\[url=(.*?)\](.*?)\[\/url\]/is", "/\[img\](.*?)\[\/img\]/is", "/\[quote\](.*?)\[\/quote\]/is", "/\[code\](.*?)\[\/code\]/is", "/\[(size|color)=(.*?)\](.*?)\[\/\\1\]/is", "/\[br\]/i", //Emoticons "/ \:\) /", "/ \:\( /", "/ \ /", "/ \ /", "/ \:\| /", "/ \ /", "/ \:\? /", "/ \;\) /"); $replacements = array( //BB Code "<b>\\1</b>", "<i>\\1</i>", "<u>\\1</u>", "<s>\\1</s>", "<marquee>\\1</marquee>", "<a href=\"\\1\">\\1</a>", "<a href=\"\\1\" target=\"_blank\">\\2</a>", "<img border=\"0\" src=\"\\1\">", "<div><b>Quote:</b> <i>\\1</i></div>", "<br /><b>Code:</b><br /><div style=\"overflow:auto;\"><xmp>\\1</xmp></div><br />", "<font \\1=\"\\2\">\\3</font>", "<br />", //Emoticons " <img src=\"smilies/happy.gif\" border=\"0\"> ", " <img src=\"smilies/angry.gif\" border=\"0\"> ", " <img src=\"smilies/omg.gif\" border=\"0\"> ", " <img src=\"smilies/tounge.gif\" border=\"0\"> ", " <img src=\"smilies/dry.gif\" border=\"0\"> ", " <img src=\"smilies/biggrin.gif\" border=\"0\"> ", " <img src=\"smilies/confused.gif\" border=\"0\"> ", " <img src=\"smilies/wink.gif\" border=\"0\"> " ); $string = "[b]This is bold text[/b] [i]this is italic text[/i] [b][i]This is bold, italic text[/b][/i]"; $result = preg_replace($patterns,$replacements,$string); echo $result; ?>[/ode] Link to comment https://forums.phpfreaks.com/topic/200700-preg_replace-question/#findComment-1053192 Share on other sites More sharing options...
Gordon Werner Posted May 4, 2010 Author Share Posted May 4, 2010 thanks folks for the help! Link to comment https://forums.phpfreaks.com/topic/200700-preg_replace-question/#findComment-1053338 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.