gacon Posted November 16, 2006 Share Posted November 16, 2006 I am writing a short function, that will change the textcolor if the texts are in the bracket, such as <Text need in red color>, then the rest of the text that are not in the bracket, will be in black color, I tried but no work at all, any ideas what wrong with the codes. Thank you.<?function WriteText($TextColor, [color=red]$Color[/color]){ $TextColor = str_replace("<",'<font style="color: [color=red]#$Color[/color]; font-weight: bold; FONT-SIZE=18pt;">', "$TextColor"); $TextColor = str_replace(">", '</font>', "$TextColor"); return ($TextColor);}$text = "<If texts inside the brackets, then assigns to new color> If texts are outside of it, will be default color.";echo WriteText("$text", "#F11A1A");?> Link to comment https://forums.phpfreaks.com/topic/27395-text-color/ Share on other sites More sharing options...
doni49 Posted November 16, 2006 Share Posted November 16, 2006 What you're looking for is something a little more powerful than straight out str_replace.preg_replace() is your function.www.php.net/preg_replace Link to comment https://forums.phpfreaks.com/topic/27395-text-color/#findComment-125291 Share on other sites More sharing options...
doni49 Posted November 16, 2006 Share Posted November 16, 2006 Well when I thought more about it,I came up with the following:[code]<?php$temp = '~`~';$TextColor = "Hello how are you? <This should be red> is it? How about <this one>?";$TextColor = str_replace('>',$temp,$TextColor);$Color = 'C80000';$TextColor = str_replace('<','<font style="color: #'. $Color .'; font-weight: bold; FONT-SIZE=18pt;">',$TextColor);$TextColor = str_replace($temp,'</font>',$TextColor);echo $TextColor;?>[/code]It temporarily changes ">" to a temporary string. Because when you replace "<" with the font string, you ADD another ">". We don't want THAT one to be changed. Link to comment https://forums.phpfreaks.com/topic/27395-text-color/#findComment-125332 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.