PHP Preg_replace and Call of Duty Colour Issue

2019-06-01 08:57发布

Call of Duty and Quake Games uses Codes form ^0 to ^1 to define colour in Names (Strings). I'm working on a custom Web RCON Script and having issues replacing the ^0 ~ ^9 with HTML Colors for output

Im Trying to Replace e.g the following in PHP

the string is generated and looks similar to Lets Say eg.

^3THE^7::^5MODERNWARFARE^7::^3Server

Im using 2 arrays for this

$find = array(  
    '/\^0(.*?)\^/',
    '/\^1(.*?)\^/',
    '/\^2(.*?)\^/',
    '/\^3(.*?)\^/',
    '/\^4(.*?)\^/',
    '/\^5(.*?)\^/',
    '/\^6(.*?)\^/',
    '/\^7(.*?)\^/',
    '/\^8(.*?)\^/',
    '/\^9(.*?)\^/',
    );              
    $replace = array(
    '<font color="#000000">$1</font>^',
    '<font color="#F65A5A">$1</font>^',
    '<font color="#00F100">$1</font>^',
    '<font color="#EFEE04">$1</font>^',
    '<font color="#0F04E8">$1</font>^',
    '<font color="#04E8E7">$1</font>^',
    '<font color="#F75AF6">$1</font>^',
    '<font color="#FFFFFF">$1</font>^',
    '<font color="#7E7E7E">$1</font>^',
    '<font color="#6E3C3C">$1</font>^',
    );

    // Just a Random Test String
    $aDemoString = "^3THE^7::^5MODERNWARFARE^7::^3Server^7";

    $namefix     = preg_replace($find, $replace, $aDemoString);

    echo $namefix;

The Output only partially works, and i get

<font color="#00F100">THE</font><font color="#FFFFFF">::<font color="#04E8E7">MODERNWARFARE</font></font>^7::<font color="#00F100">Server</font>^7

and it breaks the closing </font> tag

if i remove a ^7 to the end of the string i get

<font color="#00F100">THE</font><font color="#FFFFFF">::<font color="#04E8E7">MODERNWARFARE</font></font>^7::^2Server

It doesnt appear to replace the ^7 in the string correctly and for some reason messes up the html </font> end tag also if i remove the last ^7 then it breaks the last "^2" replacement

My Question is How Do I get PHP Preg_replace to do this correctly, any Help ?

4条回答
2楼-- · 2019-06-01 09:06

instead of ^ you can replace like this

$find = array('/\^3(.*?).\d+/',.........);

$replace = array(
    '<font color="#000000">$1</font>',........);
查看更多
小情绪 Triste *
3楼-- · 2019-06-01 09:10

The patern is ^0STRING^ so you should remove the number at the end, to make all of the parts 'work' (and have the same patern).

So first you should do this:

$aDemoString = "^3THE^7::^5MODERNWARFARE^7::^3Server^";

That is part one, then you should make sure that the ^-character isn't included in the replacing:

$find = array(  
    '/\^0([^\^]+)\^/',
    '/\^1([^\^]+)\^/',
    '/\^2([^\^]+)\^/',
    '/\^3([^\^]+)\^/',
    '/\^4([^\^]+)\^/',
    '/\^5([^\^]+)\^/',
    '/\^6([^\^]+)\^/',
    '/\^7([^\^]+)\^/',
    '/\^8([^\^]+)\^/',
    '/\^9([^\^]+)\^/'
); 
查看更多
Evening l夕情丶
4楼-- · 2019-06-01 09:12

Try this:

$colors = array(
    "#000000",
    "#F65A5A",
    "#00F100",
    "#EFEE04",
    "#0F04E8",
    "#04E8E7",
    "#F75AF6",
    "#FFFFFF",
    "#7E7E7E",
    "#6E3C3C",
);
$find = array(
    '/\^(\d)([^\^]*)/e',
); 
$replace = array(
    '"<font color=\"".$colors["$1"]."\">$2</font>"',
);

// Just a Random Test String
$aDemoString = "^3THE^7::^5MODERN^2WARFARE^7::^3Server^7";

$namefix     = preg_replace($find, $replace, $aDemoString);
echo $namefix."\n";
查看更多
5楼-- · 2019-06-01 09:15
<?php
$find = array(  
    '/\^0([^^\d]*)/',
    '/\^1([^^\d]*)/',
    '/\^2([^^\d]*)/',
    '/\^3([^^\d]*)/',
    '/\^4([^^\d]*)/',
    '/\^5([^^\d]*)/',
    '/\^6([^^\d]*)/',
    '/\^7([^^\d]*)/',
    '/\^8([^^\d]*)/',
    '/\^9([^^\d]*)/',
    );              
    $replace = array(
    '<font color="#000000">',
    '<font color="#F65A5A">',
    '<font color="#00F100">',
    '<font color="#EFEE04">',
    '<font color="#0F04E8">',
    '<font color="#04E8E7">',
    '<font color="#F75AF6">',
    '<font color="#FFFFFF">',
    '<font color="#7E7E7E">',
    '<font color="#6E3C3C">',
    );


    // Just a Random Test String
    $aDemoString = "^3THE^7::^5MODERNWARFARE^7::^3Server^7";
    preg_match_all('/\^(\d)([^^\d]*)/', $aDemoString, $out);

    print_r($out);
    foreach($out[0] as $key => $val){
      $aDemoString = str_replace($val, $replace[$out[1][$key]].$out[2][$key].'</font>', $aDemoString);
    }


    echo ($aDemoString);
    //<font color="#EFEE04">THE</font><font color="#FFFFFF">::</font><font color="#04E8E7">MODERNWARFARE</font><font color="#FFFFFF">::</font><font color="#EFEE04">Server</font><font color="#FFFFFF"></font>
?>
查看更多
登录 后发表回答