redbrad0 Posted August 13, 2008 Share Posted August 13, 2008 I have very large strings that I am needing to search for [color:xxxxxx] and replace it with <font color=xxxx> where the xxxx can be anything. So I created a while loop until I have found and replaced all [color:. The problem is that this script seems to run very slow. Is there a way to speed it up? // Try and handle the [color:] tag $tmpColorComplete = false; $tmpI = 0; while(!$tmpColorComplete) { $tmpColorIStart = strpos($htmlversion,"[color:"); if ($tmpColorIStart<0) { $tmpColorComplete = true; } else { $tmpColorIEnd = strpos($htmlversion,"]",$tmpColorI); $tmpColor = substr($htmlversion,($tmpColorIStart+7),($tmpColorIEnd-($tmpColorIStart+7))); $tmpHTMLStart = substr($htmlversion,0,$tmpColorIStart); $tmpHTMLEnd = substr($htmlversion,($tmpColorIEnd+1),strlen($htmlversion)); $htmlversion = $tmpHTMLStart . "<font color=" . $tmpColor . ">" . $tmpHTMLEnd; $tmpColorComplete = false; } if ($tmpI>100) $tmpColorComplete = true; $tmpI++; } Quote Link to comment Share on other sites More sharing options...
efficacious Posted August 13, 2008 Share Posted August 13, 2008 you might be able to speed it up by using regular expressions. instead of searching for the beginng and ending tags seperately.. I'm no good with regexes but I kno it could work for you. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 13, 2008 Share Posted August 13, 2008 <?php $string = "[color:FFFFFF]Testing![/color]"; $string = preg_replace('#\[color:([a-z0-9]{6})\](.+?)\[/color\]#i', '<font color="$1">$2</font>', $string); echo $string; ?> Quote Link to comment Share on other sites More sharing options...
thebadbad Posted August 13, 2008 Share Posted August 13, 2008 Freakin' thunderstorm wrecking my internet.. But here's my post at last. efficacious is right, and below's a regex that should work. I guess you're looking for instances of [ color:* ] text to color [ /color ] (without spaces)? And the font tag is outdated, use a span tag instead, with CSS coloring: <?php function bb2html($text) { $find = array( '~\[color:([#a-zA-Z0-9]*?)\](.*?)\[/color\]~s' ); $replace = array( '<span style="color: $1;">$2</span>' ); return preg_replace($find, $replace, $text); } //usage $text = '[color:red]colored text[/color]'; echo bb2html($text); //or $text = '[color:#ff00ff]colored text[/color]'; echo bb2html($text); ?> I wrote the function so it's easy to append new BB codes. It could be a bit simpler if you're only looking for the single [ color ] thingy. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 13, 2008 Share Posted August 13, 2008 I think he just wanted [ color:HEXCODE ]sometext[ /color ], which is why I just used {6} on the opening tag. >_< Quote Link to comment Share on other sites More sharing options...
efficacious Posted August 13, 2008 Share Posted August 13, 2008 good thinking but what if users choose to put a text based color name in as the value? (3letrs) Quote Link to comment Share on other sites More sharing options...
efficacious Posted August 13, 2008 Share Posted August 13, 2008 hehe sry... /[ color=red /] 3ltrs/[ \ color /] Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 13, 2008 Share Posted August 13, 2008 good thinking but what if users choose to put a text based color name in as the value? (3letrs) That's not what I got out of his first post, but point taken. Use: <?php $string = "[color:FFFFFF]Testing![/color]"; $string = preg_replace('#\[color:([#a-z0-9]+?)\](.+?)\[/color\]#i', '<font color="$1">$2</font>', $string); echo $string; ?> Quote Link to comment Share on other sites More sharing options...
redbrad0 Posted August 13, 2008 Author Share Posted August 13, 2008 I tried to use both of these and although it replaced some code, it didn't replace all of them. Here are both samples. Any other ideas? Thanks for your help so far $string_to_convert = "[h1] [center][color:red]Some Title Here [color:black][i][u]Other Color[/color][/i][/u] with [u]Underlined[/u] Text![/h1][/center] [/color]"; $htmlversion = test_EmailHTMLVersion($string_to_convert); echo $htmlversion; function test_EmailHTMLVersion($newText) { $htmlversion = $newText; $htmlversion = str_replace("[b]","<b>",$htmlversion); $htmlversion = str_replace("[/b]","</b>",$htmlversion); $htmlversion = str_replace("[i]","<i>",$htmlversion); $htmlversion = str_replace("[/i]","</i>",$htmlversion); $htmlversion = str_replace("[u]","<u>",$htmlversion); $htmlversion = str_replace("[/u]","</u>",$htmlversion); $htmlversion = str_replace("[s]","<s>",$htmlversion); $htmlversion = str_replace("[/s]","</s>",$htmlversion); $htmlversion = str_replace(" [left]","<div align=left>",$htmlversion); $htmlversion = str_replace("[/left] ","</div>",$htmlversion); $htmlversion = str_replace(" [center]","<div align=center>",$htmlversion); $htmlversion = str_replace("[/center] ","</div>",$htmlversion); $htmlversion = str_replace(" [right]","<div align=right>",$htmlversion); $htmlversion = str_replace("[/right] ","</div>",$htmlversion); $htmlversion = str_replace("[h1]","<h1>",$htmlversion); $htmlversion = str_replace("[/h1]","</h1>",$htmlversion); $htmlversion = str_replace("[h2]","<h2>",$htmlversion); $htmlversion = str_replace("[/h2]","</h2>",$htmlversion); $htmlversion = str_replace("[h3]","<h3>",$htmlversion); $htmlversion = str_replace("[/h3]","</h3>",$htmlversion); $htmlversion = str_replace("[h4]","<h4>",$htmlversion); $htmlversion = str_replace("[/h4]","</h4>",$htmlversion); $htmlversion = str_replace("[h5]","<h1>",$htmlversion); $htmlversion = str_replace("[/h5]","</h5>",$htmlversion); $htmlversion = str_replace("[hr]","<hr>",$htmlversion); // Try and handle the [color:] tag $find = array('~\[color:([#a-zA-Z0-9]*?)\](.*?)\[/color\]~s'); $replace = array('<span style="color: $1;">$2</span>'); $htmlversion = preg_replace($find, $replace, $htmlversion); //$htmlversion = preg_replace('#\[color:([a-z0-9]{6})\](.+?)\[/color\]#i', '<font color="$1">$2</font>', $htmlversion); $htmlversion = str_replace("\r\n","<br>\r\n",$htmlversion); return $htmlversion; } echo "<br><br>Test #2<br><br>"; $htmlversion = test_EmailHTMLVersion_2($string_to_convert); echo $htmlversion; function test_EmailHTMLVersion_2($newText) { $htmlversion = $newText; $htmlversion = str_replace("[b]","<b>",$htmlversion); $htmlversion = str_replace("[/b]","</b>",$htmlversion); $htmlversion = str_replace("[i]","<i>",$htmlversion); $htmlversion = str_replace("[/i]","</i>",$htmlversion); $htmlversion = str_replace("[u]","<u>",$htmlversion); $htmlversion = str_replace("[/u]","</u>",$htmlversion); $htmlversion = str_replace("[s]","<s>",$htmlversion); $htmlversion = str_replace("[/s]","</s>",$htmlversion); $htmlversion = str_replace(" [left]","<div align=left>",$htmlversion); $htmlversion = str_replace("[/left] ","</div>",$htmlversion); $htmlversion = str_replace(" [center]","<div align=center>",$htmlversion); $htmlversion = str_replace("[/center] ","</div>",$htmlversion); $htmlversion = str_replace(" [right]","<div align=right>",$htmlversion); $htmlversion = str_replace("[/right] ","</div>",$htmlversion); $htmlversion = str_replace("[h1]","<h1>",$htmlversion); $htmlversion = str_replace("[/h1]","</h1>",$htmlversion); $htmlversion = str_replace("[h2]","<h2>",$htmlversion); $htmlversion = str_replace("[/h2]","</h2>",$htmlversion); $htmlversion = str_replace("[h3]","<h3>",$htmlversion); $htmlversion = str_replace("[/h3]","</h3>",$htmlversion); $htmlversion = str_replace("[h4]","<h4>",$htmlversion); $htmlversion = str_replace("[/h4]","</h4>",$htmlversion); $htmlversion = str_replace("[h5]","<h1>",$htmlversion); $htmlversion = str_replace("[/h5]","</h5>",$htmlversion); $htmlversion = str_replace("[hr]","<hr>",$htmlversion); // Try and handle the [color:] tag $htmlversion = preg_replace('#\[color:([a-z0-9]{6})\](.+?)\[/color\]#i', '<font color="$1">$2</font>', $htmlversion); $htmlversion = str_replace("\r\n","<br>\r\n",$htmlversion); return $htmlversion; } Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 13, 2008 Share Posted August 13, 2008 You can't nest the colors like that. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted August 13, 2008 Share Posted August 13, 2008 Yeah, your opening and closing of tags is messed up. If you want to be able to handle nested tags, have a look at this thread, on handling the opening and closing tags separately. Quote Link to comment Share on other sites More sharing options...
effigy Posted August 13, 2008 Share Posted August 13, 2008 An aside from the main post: Utilize the fact that str_replace accepts arrays to clean up your code, or use regex. For example, preg_replace('/\[(\/?h\d)\]/', '<$1>', $str); will take care of all the h# tags, opening and closing. You can expand the h into a character class or alternation to handle the other tags. Quote Link to comment Share on other sites More sharing options...
redbrad0 Posted August 13, 2008 Author Share Posted August 13, 2008 I tried to look at that code on the linked thread but since I am not sure the exacts on regexp can you help me just change the [color:xxxx] to the span color? I can then just use the str_replace to replace the [/color] tag. $string_to_convert = "[h1] [center][color:red]Some Title Here [color:black][i][u]Other Color[/color][/i][/u] with [u]Underlined[/u] Text![/h1][/center] [/color]"; $htmlversion = replace_colorHTML($string_to_convert); echo $htmlversion; function replace_colorHTML($newText) { $newText = preg_replace('#\[color:([a-z0-9]{6})\]#i', '<span style="color: $1;">', $newText); return $newText; } Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 13, 2008 Share Posted August 13, 2008 function replace_colorHTML($newText) { $newText = preg_replace('#\[color:([a-z0-9]{6})\]#i', '<span style="color: #$1;">', $newText); $newText = str_replace('[/color]', '</span>', $newText); return $newText; } Quote Link to comment Share on other sites More sharing options...
redbrad0 Posted August 13, 2008 Author Share Posted August 13, 2008 Thanks guys for your quick replys for something that seems so easy. I guess after this I will need to start learning regexp. DarkWater.. I tried your code and it doesnt seem to replace the color $string_to_convert = "[h1] [center][color:red]Some Title Here [color:black][i][u]Other Color[/color][/i][/u] with [u]Underlined[/u] Text![/h1][/center] [/color]"; $htmlversion = replace_colorHTML($string_to_convert); echo $htmlversion; function replace_colorHTML($newText) { // Replace all the starting color tags $newText = preg_replace('#\[color:([a-z0-9]{6})\]#i', '<span style="color: #$1;">', $newText); $newText = str_replace('[/color]', '</span>', $newText); return $newText; } Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 13, 2008 Share Posted August 13, 2008 Sorry, I thought you just wanted hex values in there, not color names. Try: function replace_colorHTML($newText) { // Replace all the starting color tags $newText = preg_replace('#\[color:([a-z0-9#]+?)\]#i', '<span style="color: $1;">', $newText); $newText = str_replace('[/color]', '</span>', $newText); return $newText; } Quote Link to comment Share on other sites More sharing options...
redbrad0 Posted August 13, 2008 Author Share Posted August 13, 2008 $newText = preg_replace('#\[color:([a-z0-9#]+?)\]#i', '<span style="color: $1;">', $newText); PHP Warning: preg_replace(): Unknown modifier ']' Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 13, 2008 Share Posted August 13, 2008 Woops. $newText = preg_replace('$\[color:([a-z0-9#]+?)\]$i', '<span style="color: $1;">', $newText); Quote Link to comment Share on other sites More sharing options...
effigy Posted August 14, 2008 Share Posted August 14, 2008 Keep it simple: /\[color:([^\]]+)\]/i. 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.