String Replace with font color

Discussion in 'PHP' started by KJThaDon, Aug 4, 2009.

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

     
    <?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";
    ?>
     
    Code (text):
     
    KJThaDon, Aug 4, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php
    $search = array('^1', '^2', '^3');
    $replace = array('RED', 'GREEN', 'YELLOW');
    
    $text = str_replace($search, $replace, $text);
    ?>
    PHP:
    And so on for the other colors.
     
    premiumscripts, Aug 4, 2009 IP
  3. KJThaDon

    KJThaDon Member

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    Is that going to display the word RED GREEN ETC not output that color correct?

    I need to have ^1 replaced with a font color etc

    Thanks
     
    Last edited: Aug 4, 2009
    KJThaDon, Aug 4, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Why don't you test it?
     
    premiumscripts, Aug 4, 2009 IP
  5. KJThaDon

    KJThaDon Member

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    I am its not displaying server name at all
     
    KJThaDon, Aug 4, 2009 IP
  6. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Oh, I misread your question:

    <?php
    $search = array('^1', '^2', '^3');
    $replace = array('<font color="red">', '<font color="green">', 'font color="yellow">');
    
    $text = '^1Server ^2Come Join!';
    
    $cnt = 0;
    $text = str_replace($search, $replace, $text, $cnt);
    
    if ($cnt) {
       $text = preg_replace('/<font/i', '</font><font', $text);
       $text = substr($text, 7);
       $text .= '</font>';
    }
    
    echo $text;
    ?>
    PHP:
     
    premiumscripts, Aug 4, 2009 IP
  7. KJThaDon

    KJThaDon Member

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    
    <?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);
    ?>
    PHP:

    Just got it working like this
    Thanks for the help!
     
    KJThaDon, Aug 5, 2009 IP
  8. cignusweb

    cignusweb Peon

    Messages:
    147
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    thanx I have a same problem
     
    cignusweb, Aug 5, 2009 IP