String Replace with font color

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stoggafu
Forum Newbie
Posts: 5
Joined: Sun Jan 04, 2009 5:17 am

String Replace with font color

Post by stoggafu »

I am grabbing a game server's name, and displaying it on a page.
In the game configuration files on windows, you can set your server name with colors

Example:

^1 - RED
^2 - GREEN
^3 - YELLOW
^4 - BLUE
^5 - CYAN
^6 - PINK
^7 - WHITE
^8 - DEFAULT MAP COLOR
^9 - GREY OR DEFAULT MAP COLOR
^0 - BLACK

So if i set my server name as ^1Server ^4Come Join!
It would show up as Server Come Join! in the server list.

Now I have got the server name displaying in a page, and replaced the ^# signs with a blank entry
$hostname will display the server name, but with the color coded characters, which of course I would not want displaying
I would instead like to have them display the appropriate color

How would I go about replacing ^0 with black and so on.
Thanks

Code: Select all

 
<?php
$text = "$hostname";
$text = str_replace("^0","", $text);
$text = str_replace("^1","", $text);
$text = str_replace("^2","", $text);
$text = str_replace("^3","", $text);
$text = str_replace("^4","", $text);
$text = str_replace("^5","", $text);
$text = str_replace("^6","", $text);
$text = str_replace("^7","", $text);
$text = str_replace("^8","", $text);
$text = str_replace("^9","", $text);
echo "$text";
?>
 
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: String Replace with font color

Post by prometheuzz »

stoggafu wrote:I am grabbing a game server's name, and displaying it on a page.
In the game configuration files on windows, you can set your server name with colors

Example:

^1 - RED
^2 - GREEN
^3 - YELLOW
^4 - BLUE
^5 - CYAN
^6 - PINK
^7 - WHITE
^8 - DEFAULT MAP COLOR
^9 - GREY OR DEFAULT MAP COLOR
^0 - BLACK

So if i set my server name as ^1Server ^4Come Join!
It would show up as Server Come Join! in the server list.

Now I have got the server name displaying in a page, and replaced the ^# signs with a blank entry
$hostname will display the server name, but with the color coded characters, which of course I would not want displaying
I would instead like to have them display the appropriate color

How would I go about replacing ^0 with black and so on.
Thanks

Code: Select all

 
<?php
$text = "$hostname";
$text = str_replace("^0","", $text);
$text = str_replace("^1","", $text);
$text = str_replace("^2","", $text);
$text = str_replace("^3","", $text);
$text = str_replace("^4","", $text);
$text = str_replace("^5","", $text);
$text = str_replace("^6","", $text);
$text = str_replace("^7","", $text);
$text = str_replace("^8","", $text);
$text = str_replace("^9","", $text);
echo "$text";
?>
 
That is not something str_replace can handle. Try something like this:

Code: Select all

$regexes = array('/\^1([^^]++)/', '/\^4([^^]++)/');
$replacements = array('<RED>$1</RED>', '<BLUE>$1</BLUE>');
$text = 'So if i set my server name as ^1Server ^4Come Join!';
echo preg_replace($regexes, $replacements, $text);
/* output:
So if i set my server name as <RED>Server </RED><BLUE>Come Join!</BLUE>
*/
stoggafu
Forum Newbie
Posts: 5
Joined: Sun Jan 04, 2009 5:17 am

Re: String Replace with font color

Post by stoggafu »

Thank you but the output is not colored for me
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: String Replace with font color

Post by prometheuzz »

stoggafu wrote:Thank you but the output is not colored for me
Well, you need to change <RED> and </RED> into something else of course. My example was meant to show you how the match a '^' followed by some other text and then use an array of replacements to change it in one go.
Last edited by prometheuzz on Wed Aug 05, 2009 2:03 am, edited 1 time in total.
stoggafu
Forum Newbie
Posts: 5
Joined: Sun Jan 04, 2009 5:17 am

Re: String Replace with font color

Post by stoggafu »

I got it worked out like this thanks a lot!

Code: Select all

<?php
$regexes = array('/\^1([^^]++)/', '/\^2([^^]++)/', '/\^3([^^]++)/', '/\^4([^^]++)/', '/\^5([^^]++)/', '/\^6([^^]++)/', '/\^7([^^]++)/');
$replacements = array('<font color="red">$1</font>', '<font color="green">$1</font>', '<font color="yellow">$1</font>', '<font color="blue">$1</font>', '<font color="cyan">$1</font>', '<font color="pink">$1</font>', '<font color="white">$1</font>');
$hostname = $hostname;
echo preg_replace($regexes, $replacements, $hostname);
?>
 
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: String Replace with font color

Post by prometheuzz »

stoggafu wrote:I got it worked out like this thanks a lot!

...
Good, and you're welcome.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: String Replace with font color

Post by onion2k »

Font tags are depreciated.
Post Reply