Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php将字符串中的{rrggbb}颜色替换为带<;span>;_Php_Regex - Fatal编程技术网

php将字符串中的{rrggbb}颜色替换为带<;span>;

php将字符串中的{rrggbb}颜色替换为带<;span>;,php,regex,Php,Regex,我从一个游戏中收到文本,该游戏使用文本中的颜色嵌入来为消息着色,您只需这样使用:{ff0000}红色文本{00ff00}绿色文本 我想通过php输出这样的字符串,并将其正确着色。 所以上面的字符串变成了这个 <span style="color: #ff0000">red text </span><span style="color: #00ff00">green text</span> 红色文本绿色文本 我对此有一些想法(比如我知道第一个颜色

我从一个游戏中收到文本,该游戏使用文本中的颜色嵌入来为消息着色,您只需这样使用:{ff0000}红色文本{00ff00}绿色文本

我想通过php输出这样的字符串,并将其正确着色。 所以上面的字符串变成了这个

<span style="color: #ff0000">red text </span><span style="color: #00ff00">green text</span>
红色文本绿色文本
我对此有一些想法(比如我知道第一个颜色匹配需要替换为
,下一次出现的任何颜色都需要替换为
,以关闭前一个span标记,如果至少找到一个匹配,则需要在字符串末尾添加另一个span closing标记)。 我可能会把这件事复杂化,用循环和比较文本手动完成,但那会很复杂,而且可能效率低下


如何使用php中的一些正则表达式函数(可能还有一个循环)实现这一点?

您可以尝试以下方法:

$str = '{ff0000}red text {00ff00}green text' ;
$str = preg_replace_callback('~\{([^\}]*)\}([^\{]+)~', function($matches) {
    return '<span style="color:#'.$matches[1].'">'.$matches[2].'</span>' ;
}, $str) ;
echo $str ; 
$str='{ff0000}红色文本{00ff00}绿色文本';
$str=preg\u replace\u回调('~\{([^\}]*)\}([^\{]+)~',函数($matches){
返回“.$matches[2]”;
}美元/平方米);
echo$str;
产出:

<span style="color:#ff0000">red text </span><span style="color:#00ff00">green text</span>
红色文本绿色文本

您可以尝试以下方法:

$str = '{ff0000}red text {00ff00}green text' ;
$str = preg_replace_callback('~\{([^\}]*)\}([^\{]+)~', function($matches) {
    return '<span style="color:#'.$matches[1].'">'.$matches[2].'</span>' ;
}, $str) ;
echo $str ; 
$str='{ff0000}红色文本{00ff00}绿色文本';
$str=preg\u replace\u回调('~\{([^\}]*)\}([^\{]+)~',函数($matches){
返回“.$matches[2]”;
}美元/平方米);
echo$str;
产出:

<span style="color:#ff0000">red text </span><span style="color:#00ff00">green text</span>
红色文本绿色文本

这根本不是HTML解析,正则表达式是这里最合适的工具:

{([a-fA-F0-9]+)}((?>[^{]*{?(?!(?1)}))*)
细分:

{   # Match `{`
    ([a-fA-F0-9]+) # Match and capture hex color code
}   # Match `}`
(   # Capturing group #2
    (?> # Start of a non-capturing group (atomic)
        [^{]*   # Match anything up to a `{`
        {?(?!(?1)}) # Match it if not going to contain a color code
    )*  # As much as possible, end of NCG
)   # End of CG #2

PHP代码:

$text = <<< 'TXT'
{ff0000}red tex {00ff00}green text
TXT;

echo preg_replace('~{([a-fA-F0-9]+)}((?>[^{]*{?(?!(?1)}))*)~',
        '<span style="color: #$1">$2</span>', $text);

$text=这根本不是HTML解析,正则表达式是这里最合适的工具:

{([a-fA-F0-9]+)}((?>[^{]*{?(?!(?1)}))*)
细分:

{   # Match `{`
    ([a-fA-F0-9]+) # Match and capture hex color code
}   # Match `}`
(   # Capturing group #2
    (?> # Start of a non-capturing group (atomic)
        [^{]*   # Match anything up to a `{`
        {?(?!(?1)}) # Match it if not going to contain a color code
    )*  # As much as possible, end of NCG
)   # End of CG #2

PHP代码:

$text = <<< 'TXT'
{ff0000}red tex {00ff00}green text
TXT;

echo preg_replace('~{([a-fA-F0-9]+)}((?>[^{]*{?(?!(?1)}))*)~',
        '<span style="color: #$1">$2</span>', $text);

$text=使用Dom解析器容易得多。@JaredFarrish不是Dom解析器的工作。使用Dom解析器容易得多。@JaredFarrish不是Dom解析器的工作。如果文本包含Unicode字符、符号……标点符号怎么办?@revo你说得对。我知道。这是给定示例的解决方案。谢谢这个有趣的问题。Anwser更新d使用其他字符…除了
{
。现在它变得更好了。+1如果文本包含Unicode字符、符号…标点符号怎么办?@revo你是对的。我知道。这是给定示例的解决方案。感谢这个有趣的问题。Anwser更新为使用其他字符…除了
{
。现在好多了。+1漂亮的安瑟和解释:)非常感谢你这个充满教训的答案!漂亮的安瑟和解释:)非常感谢你这个充满教训的答案!