Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I have a String, : $s="[#efefef]H[#fafafa]I!";
$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.
<font>
<span style="color: #efefef;">
preg_replace("/\\[#([0-9a-f]+)\\]([^[]+)/i", "<span style=\"color: #\\1;\">\\2</span>", $s);
Add a comment
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.
[A-Z]
Required, but never shown
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.
Explore related questions
See similar questions with these tags.