Jump to content

Preg_Replace Help


Tenaciousmug

Recommended Posts

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..

Link to comment
https://forums.phpfreaks.com/topic/269679-preg_replace-help/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/269679-preg_replace-help/#findComment-1386380
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.