Need help with this piece of code

Discussion in 'PHP' started by x0x, Jul 3, 2011.

  1. #1
    This piece of code makes the text colorful... Each character has its own color:
    
    function color($text)
    {
        /*** initialize the return string ***/
        $ret = '';
    
        /*** an array of colors ***/
        $colors = array(
            'ff00ff',
            'ff00cc',
            'ff0099',
            'ff0066',
            'ff0033',
            'ff0000',
            'ff3300',
            'ff6600',
            'ff9900',
            'ffcc00',
            'ffff00',
            'ccff00',
            '99ff00',
            '66ff00',
            '33ff00',
            '00ff00',
            '00ff33',
            '00ff66',
            '00ff99',
            '00ffcc',
            '00ffff',
            '00ccff',
            '0099ff',
            '0066ff',
            '0033ff',
            '0000ff',
            '3300ff',
            '6600ff',
            '9900ff',
            'cc00ff');
        /*** a counter ***/
        $i = 0;
    
        /*** get the length of the text ***/
        $textlength = strlen($text);
    
        /*** loop over the text ***/
        while($i<=$textlength)
        {
            /*** loop through the colors ***/
            foreach($colors as $value)
            {
                if ($text[$i] != "")
                {
                    $ret .= '<span style="color:#'.$value.';">'.$text[$i]."</span>";
                }
            $i++;
            }
        }
        /*** return the highlighted string ***/
        return $ret;
    }
    PHP:
    It's working almost perfectly. The problem is it makes the other tags unusable. [bold] for example...

    Would someone be able to guide me or edit this code to make it skip everything between [ and ] ? that includes [bold]text[/bold] (while "text" still getting colored), but not the tags because they are used by another function.
     
    x0x, Jul 3, 2011 IP
  2. phpgod

    phpgod Member

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #2
    Use this revised function instead :-
    
    function color($text,$otag,$ctag)
    {
        /*** initialize the return string ***/
        $ret = '';
    
        /*** an array of colors ***/
        $colors = array(
            'ff00ff',
            'ff00cc',
            'ff0099',
            'ff0066',
            'ff0033',
            'ff0000',
            'ff3300',
            'ff6600',
            'ff9900',
            'ffcc00',
            'ffff00',
            'ccff00',
            '99ff00',
            '66ff00',
            '33ff00',
            '00ff00',
            '00ff33',
            '00ff66',
            '00ff99',
            '00ffcc',
            '00ffff',
            '00ccff',
            '0099ff',
            '0066ff',
            '0033ff',
            '0000ff',
            '3300ff',
            '6600ff',
            '9900ff',
            'cc00ff');
        /*** a counter ***/
        $i = 0;
    
        /*** get the length of the text ***/
        $textlength = strlen($text);
    
        /*** loop over the text ***/
        while($i<=$textlength)
        {
            /*** loop through the colors ***/
            foreach($colors as $value)
            {
                if ($text[$i] != "")
                {
                    $ret .= '<span style="color:#'.$value.';">'.$otag.$text[$i].$ctag."</span>";
                }
            $i++;
            }
        }
        /*** return the highlighted string ***/
        return $ret;
    }
    
    PHP:
    Now, you just have to pass the text to be colored, opening tags of other styles you want to apply and then closing tags in appropriate order, for e.g :-

    $coloredtext=color($text,"<b><i>","</b></i>"); // you can pass as many tags as you wish but mention closing tags in appropriate order
    PHP:
    Hope this helps !!
     
    phpgod, Jul 4, 2011 IP
  3. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #3
    It does work but it's not exactly what I'm looking for. The tags are sent by the user.

    For example. User input:

    <b>test</b>

    This will not work when it's sent through the color function... The color function does not ignore the tags and makes them "colorful" as well, making the tags unusable. The best way would be to make that function ignore everything between <tag> and </tag>.. I'm guessing a regex is needed. That is my weak spot...
     
    x0x, Jul 4, 2011 IP