stitchmedia Posted March 11, 2010 Share Posted March 11, 2010 I am new to this regex stuff so am still on a massive learning curve. I have a file called bbcode.php which contains all of my regex to convert BbCode to HTML but I dont know how to convert the tag. I have this line to convert the size: $text = eregi_replace("\\[size([^\\[]*)\\]([^\\[]*)\\[/size\\]","<font size=\"\\1px\">\\2</font>",$text); I could start playing around with it but I really dont know what I am doing until I have read up more about it. I also need to know how to convert the tag. Thanks Dave Quote Link to comment Share on other sites More sharing options...
cags Posted March 11, 2010 Share Posted March 11, 2010 Firstly the eregi functions are deprecated, you should consider swapping over to use the preg functions instead. Secondly, how exactly conversion of the Font and URL tags would work is very much dependent on how exactly you use the BBCode tags, I've seen multiple different techniques for using them. If you want help with Regex patterns you really need to provide example inputs, expected outputs and any tricky case that may confuse a normal pattern match. Quote Link to comment Share on other sites More sharing options...
stitchmedia Posted March 11, 2010 Author Share Posted March 11, 2010 Thanks for the reply. The regex I got as an example from somewhere else but I will try and update as you suggest. Examples are.. [font=Arial]value[/font] needs changing to something like... <span style="font:Arial">value</span> and [url=http://www.google.com]value[/url] needs changing to... <a href="http://www.google.com">value</a> Quote Link to comment Share on other sites More sharing options...
XeNoMoRpH1030 Posted March 11, 2010 Share Posted March 11, 2010 If that is the case, something like this would work: $text = preg_replace('/\[font\=(.*?)\](.*?)\[\/font\]/is', '<span style="font: $1;">$2</span>', $text); Your example would work, but also [font=15px arial,sans-serif]Test[/font] would work. For the URL, you can get really specific, but one I use is $text = preg_replace('/\[url\=(.*?)\](.*?)\[\/url\]/is','<a href="$1">$2</a>',$text); Quote Link to comment Share on other sites More sharing options...
cags Posted March 11, 2010 Share Posted March 11, 2010 There's probably several dozen examples of the url tags if you search the Regex board but... $input = '[font=Arial]value[/font]'; $pattern = '#\[font=([^]]+)]([^[]+)\[/font]#i'; $replacement = '<span style="font:$1">$2</span>'; echo preg_replace( $pattern, $replacement, $input ); $input = '[url=http://www.google.com]value[/url]'; $pattern = '#\[url=http://([^]]+)]([^[]+)\[/url]#i'; $replacement = '<a href="$1">$2</a>'; echo preg_replace( $pattern, $replacement, $input ); Edit: Beaten to the punch. Quote Link to comment Share on other sites More sharing options...
stitchmedia Posted March 11, 2010 Author Share Posted March 11, 2010 Thanks very much for both your help, all sorted now 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.