Jump to content

Rainbow Text Function: Ignore HTML


TheMayhem

Recommended Posts

Hello there,

 

I am working on a script that will take a user submitted text and display the text in rainbow. I found a very nice function on the Internet and used it as my base. Here is an example of the code.

 

function rainbow($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;
}

    $post[message] = rainbow($post[message]);

 

 

And it works very well :)

 

Here is my problem. At times $post[message] will contain some basic html such as a bold tag, user smiley (img tag), a link tag, and so forth. How can I take my function above so that it doesn't edit and add the html font color coding for anything inside html tags where it just skips over them and leaves that part alone? I need this otherwise it will breakup every html tag I might have inside $post[message] and add a font color html tag to it, thus voiding all other html in it.

Link to comment
https://forums.phpfreaks.com/topic/255543-rainbow-text-function-ignore-html/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.