开发者

PHP reg replace all [hexa] to a font color string?

开发者 https://www.devze.com 2023-02-19 11:32 出处:网络
I have a String, :$s=\"[#efefef]H[#fafafa]I!\"; How I can do a new string for this like: $s2=\"<font color=\'#efefef\'>H</font开发者_运维技巧><font color=\'#fafafa\'>I</font>

I have a String, : $s="[#efefef]H[#fafafa]I!";

How I can do a new string for this like:

$s2="<font color='#efefef'>H</font开发者_运维技巧><font color='#fafafa'>I</font>!";

Thanks


The <font> object is obsolete. Use <span style="color: #efefef;"> instead.

preg_replace("/\\[#([0-9a-f]+)\\]([^[]+)/i", "<span style=\"color: #\\1;\">\\2</span>", $s);


For you example data this should work nicely:

$s2 = preg_replace('~\[(#[0-9a-f]{6})\]([A-Z])~',
                   "<font color='$1'>$2</font>", $s);

You might want to change the [A-Z] placeholder for you needs. This only matches one uppercase letter, as in your example.

0

精彩评论

暂无评论...
验证码 换一张
取 消