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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.