PHP Preg_replace和使命召唤色彩问题 [英] PHP Preg_replace and Call of Duty Colour Issue

查看:96
本文介绍了PHP Preg_replace和使命召唤色彩问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使命召唤和地震游戏使用代码形式^ 0到^ 1来定义名称(字符串)中的颜色。我正在处理一个自定义的Web RCON脚本,并用HTML颜色替换^ 0〜^ 9以便输出



我尝试在PHP中替换如下内容



该字符串已生成,看起来类似于Lets Say eg。


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


使用2个数组作为这个

  $ 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 =#FFFAFF> $ 1< / font> ^',
'< font color =#FFFFFF> $ 1< / font> ^',
'< font color =#7E7E7E> $ 1< / font> ^',
'< font color =#6E3C3C> $ 1< / font> ^',
);

//只是一个随机测试字符串
$ aDemoString =^ 3THE ^ 7 :: ^ 5MODERNWARFARE ^ 7 :: ^ 3Server ^ 7;

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

echo $ namefix;

输出只有部分效果,我得到了

 < font color =#00F100> THE< / font>< font color =#FFFFFF> ::< font color =#04E8E7 > MODERNWARFARE< / font>< / font> ^ 7 ::< font color =#00F100>服务器< / font> ^ 7 

,并且它打破了关闭< / font> 标记



 < font color =#00F100>如果我将^ 7移除到字符串末尾;< / font>< font color =#FFFFFF> ::< font color =#04E8E7> MODERNWARFARE< / font>< / font> ^ 7 :: ^ 2Server 

它似乎并不正确地替换字符串中的^ 7,并且由于某种原因弄乱了html < / font> end tag如果我删除最后一个^ 7,那么它会打破最后的^ 2替换。是如何让PHP Preg_replace正确地做到这一点,任何帮助?

解决方案

试试这个:

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

//只是一个随机测试字符串
$ aDemoString =^ 3THE ^ 7 :: ^ 5MODERN ^ 2WARFARE ^ 7 :: ^ 3Server ^ 7;

$ namefix = preg_replace($ find,$ replace,$ aDemoString);
echo $ namefix。\\\
;


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 ?

解决方案

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";

这篇关于PHP Preg_replace和使命召唤色彩问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆