0

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

2 Answers 2

2

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

preg_replace("/\\[#([0-9a-f]+)\\]([^[]+)/i", "<span style=\"color: #\\1;\">\\2</span>", $s);
Sign up to request clarification or add additional context in comments.

2 Comments

i get this: [#efefef]H[#fafafa]I! <span style=""></span><span style=""></span> ....not work
Try now, sorry, beginner mistake by me :)
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.