Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7051711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:17:46+00:00 2026-05-28T03:17:46+00:00

i have prefixe for groupnames in a database, they consist of normal text and

  • 0

i have prefixe for groupnames in a database, they consist of normal text and color codes eg.:
&c[&aAdmin&c]. The colorcodes start with an & and then one character or a number. they end on the next colorcode or the end of the string.
There are no & as text in the string.
For now i have this function, but it only can handle one colorcode at the beginning of the string. any suggestions for a nice regex replace to handle multiple colorcodes in one string?

function mccolor($string){
$codes  = array(        "&0",
                        "&1",
                        "&2",
                        "&3",
                        "&4",
                        "&5",
                        "&6",
                        "&7",
                        "&8",
                        "&9",
                        "&a",
                        "&b",
                        "&c",
                        "&d",
                        "&e",
                        "&f");
$replace  = array(
                        '<span style="color:#000000;">',
                        '<span style="color:#0000BF;">',
                        '<span style="color:#00BF00;">',
                        '<span style="color:#00BFBF;">',
                        '<span style="color:#BF0000;">',
                        '<span style="color:#BF00BF;">',
                        '<span style="color:#BFBF00;">',
                        '<span style="color:#BFBFBF;">',
                        '<span style="color:#404040;">',
                        '<span style="color:#4040FF;">',
                        '<span style="color:#40FF40;">',
                        '<span style="color:#40FFFF;">',
                        '<span style="color:#FF4040;">',
                        '<span style="color:#FF40FF;">',
                        '<span style="color:#3F3F10;">',
                        '<span style="color:#FFFFFF;">');
return str_replace($codes, $replace, $string).'</span>';
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:17:47+00:00Added an answer on May 28, 2026 at 3:17 am

    How about:

    function mccolor($m) {
        $trans = array(
            "&0"=>'#000000;',
            "&1"=>'#0000BF;',
            "&2"=>'#00BF00;',
            "&3"=>'#00BFBF;',
            "&4"=>'#BF0000;',
            "&5"=>'#BF00BF;',
            "&6"=>'#BFBF00;',
            "&7"=>'#BFBFBF;',
            "&8"=>'#404040;',
            "&9"=>'#4040FF;',
            "&a"=>'#40FF40;',
            "&b"=>'#40FFFF;',
            "&c"=>'#FF4040;',
            "&d"=>'#FF40FF;',
            "&e"=>'#3F3F10;',
            "&f"=>'#FFFFFF;',
        );
        return '<span style="color:'.$trans[$m[1]].'">'.$m[2].'</span>';
    }
    echo preg_replace_callback('/(&[0-9a-f])([^&]+|$)/', 'mccolor', '&c[&aAdmin&c]');
    

    output:

    <span style="color:#FF4040;">[</span><span style="color:#40FF40;">Admin</span><span style="color:#FF4040;">]</span>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of calling codes for all countries(the phone number prefixes), I
I have some simple CSS3 animations here: http://chooseavirb.com/strat/ . They work fine on Firefox,
I have some legacy XML documents stored in a database as a blob, which
I have a block of text like this: Hello @Simon, I had a great
I have always prefixed my Constants with CN_ as shown below but I am
I have seen prefix N in some insert T-SQL queries. Many people have used
I'm trying to figure out how XSLT process namespace prefixes and have following example:
Have a source xml document that uses namespace containing prefixes and a default namespace.
I have a problem with using the prefix, seems like whenever I use the
I have a Drupal form array that uses a prefix of '#' to indicate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.