Tenaciousmug Posted October 19, 2012 Share Posted October 19, 2012 Okay I'm doing a typical BBCode to HTML converter except that it is converting BBCode from Neopets.. which is different from original BBCode. ANYWAY, I have a problem. I'll just post the parts that are messing up. function bb2html($string) { $match = array ( '#\[\/font\]#se', '#\[fontc\=(.*?)s\=(.*?)f\=(.*?)\]#se', '#\[fontc\=(.*?)f\=(.*?)s\=(.*?)\]#se', '#\[fonts\=(.*?)c\=(.*?)f\=(.*?)\]#se', '#\[fonts\=(.*?)f\=(.*?)c\=(.*?)\]#se', '#\[fontf\=(.*?)c\=(.*?)s\=(.*?)\]#se', '#\[fontf\=(.*?)s\=(.*?)c\=(.*?)\]#se', '#\[fontc\=(.*?)f\=(.*?)\]#se', '#\[fontc\=(.*?)s\=(.*?)\]#se', '#\[fonts\=(.*?)f\=(.*?)\]#se', '#\[fonts\=(.*?)c\=(.*?)\]#se', '#\[fonts\=(.*?)\]#se', '#\[fontf\=(.*?)\]#se', '#\[fontc\=(.*?)\]#se', ); $replace = array ( "'</font>'", "'<font color=\'\\1\' size=\'\\2\' face=\'\\3\'>'", "'<font color=\'\\1\' face=\'\\2\' size=\'\\3\'>'", "'<font size=\'\\1\' color=\'\\2\' face=\'\\3\'>'", "'<font size=\'\\1\' face=\'\\2\' color=\'\\3\'>'", "'<font face=\'\\1\' color=\'\\2\' size=\'\\3\'>'", "'<font face=\'\\1\' size=\'\\2\' color=\'\\3\'>'", "'<font color=\'\\1\' face=\'\\2\'>'", '<font color=\"\\1\" size=\"\\2\'>"', "'<font size=\'\\1\' face=\'\\2\'>'", "'<font size=\'\\1\' color=\'\\2\'>'", "'<font size=\'\\1\'>'", "'<font face=\'\\1\'>'", "'<font color=\'\\1\'>'", ); return preg_replace($match, $replace, $content); } $top_line = ' [center][fontc=#f1c232][sup]?[/sup]???[br]?[/font][fontc=#000000s=1]neoHTML[/font][fontc=#f1c232f=courier]?[br]['; $bottom_line = ']?I used to be Rich[sub]?[/sub]?[br]?[br]?[/font]Orange Draik-_-[fontc=#f1c232]??[/font][br][/center] '; $new_top_line = substr($top_line, 0, -1); $new_bottom_line = substr($bottom_line, 1); echo bb2html($new_top_line.$new_bottom_line); Okay, it's converting everything fine besides when it starts to get to the [fontc=#f1c232] in the very beginning. Since the [fontc=#000000s=1] would be listed first.. in the array.. it replaces those first. SO when it sees the [fontc=#f1c232], it replaces it with the first one that lists [fontc='something'... and doesn't put the ending > bracket on it... because it's expecting the s=1 and f=courier or something... How do I make it so that if it's ONLY [fontc=#198202], it uses the one that is meant for that and not the very first one? Because it's not putting the ending bracket on it.. Quote Link to comment https://forums.phpfreaks.com/topic/269679-preg_replace-help/ Share on other sites More sharing options...
Beeeeney Posted October 19, 2012 Share Posted October 19, 2012 Have you tried turning it off and on again? Quote Link to comment https://forums.phpfreaks.com/topic/269679-preg_replace-help/#findComment-1386345 Share on other sites More sharing options...
Tenaciousmug Posted October 19, 2012 Author Share Posted October 19, 2012 What do you mean?? Turning what off and on again? Quote Link to comment https://forums.phpfreaks.com/topic/269679-preg_replace-help/#findComment-1386349 Share on other sites More sharing options...
Jessica Posted October 19, 2012 Share Posted October 19, 2012 Try changing the order of your arrays. Quote Link to comment https://forums.phpfreaks.com/topic/269679-preg_replace-help/#findComment-1386350 Share on other sites More sharing options...
Tenaciousmug Posted October 19, 2012 Author Share Posted October 19, 2012 I did. I tried putting the [fontc=blah] first, but then it replaces the one that has [fontc=#000000s=1] to <font color="#000000s=1"> ... :/ Quote Link to comment https://forums.phpfreaks.com/topic/269679-preg_replace-help/#findComment-1386353 Share on other sites More sharing options...
requinix Posted October 19, 2012 Share Posted October 19, 2012 (edited) I'm betting that the .*? is too permissive. Try something that also restricts the input. But if I'm going to be rewriting the expression a little I might as well help with the sheer number of rules you have in there. function bb2html($string) { $match = array ( '#\[\/font\]#', '/\[font(?:c\=(#?\w+)|s\=(\d+)|f\=(\w+))+\]/e', ); $replace = array ( "</font>", "'<font' . ('\\1' ? ' color=\"\\1\"' : '') . ('\\2' ? ' size=\"\\2\"' : '') . ('\\3' ? ' face=\"\\3\"' : '') . '>'", ); return preg_replace($match, $replace, $string); } Oh, and moved to Regex. Edited October 19, 2012 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/269679-preg_replace-help/#findComment-1386380 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.