开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜