BigToach.com

PHP Snippets

gradient_text

Information

(PHP Version >= 3.0.6)
Functions used in this snippet: str_replace, strlen, hexdec, substr, dechex, implode.

Description

str gradient_text( str str, str color1, str color2, str html )

Outputs a string as a gradient from color color1 to color color2. It outputs the colors based on the tag of type html.

Snippet

<?php
function gradient_text($str$color1$color2$html='span'){
    
$color1 str_replace('#'''$color1);
    
$color2 str_replace('#'''$color2);
    
$color1 strlen($color1) === $color1{0} . $color1{0} . $color1{1} . $color1{1} . $color1{2} . $color1{2} : $color1;
    
$color2 strlen($color2) === $color2{0} . $color2{0} . $color2{1} . $color2{1} . $color2{2} . $color2{2} : $color2;
    
$endstr '<' $html ' style="color:#' $color1 ';">' $str{0} . '</' $html '>';
    
$colors[0] = hexdec(substr($color202));
    
$colors[1] = hexdec(substr($color222));
    
$colors[2] = hexdec(substr($color242));
    
$colors[3] = hexdec(substr($color102));
    
$colors[4] = hexdec(substr($color122));
    
$colors[5] = hexdec(substr($color142));
    for(
$i=1$i<strlen($str) - 1$i++){
        if(
$str{$i} != ' '){
            
$color = array();
            
$color[] = dechex(($colors[0] - $colors[3]) / strlen($str) * $i $colors[3]);
            
$color[] = dechex(($colors[1] - $colors[4]) / strlen($str) * $i $colors[4]);
            
$color[] = dechex(($colors[2] - $colors[5]) / strlen($str) * $i $colors[5]);
            
$color[0] = strlen($color[0]) < $color[0] . $color[0] : $color[0];
            
$color[1] = strlen($color[1]) < $color[1] . $color[1] : $color[1];
            
$color[2] = strlen($color[2]) < $color[2] . $color[2] : $color[2];
            
$endstr .= '<' $html ' style="color:#' implode($color,'') . '">' $str{$i} . '</' $html '>';
        }else{
            
$endstr .= ' ';
        }
    }
    
$endstr .= '<' $html ' style="color:#' $color2 ';">' $str{(strlen($str)-1)} . '</' $html '>';
    return 
$endstr;
}
?>

Example

gradient_text Can be used in the following way:

<?php
echo gradient_text('red will turn into blue''#FF0000''#0000FF');
// out puts "red will turn into blue" as a gradient from red to blue

echo gradient_text('red will turn into blue''f00''00f''font');
// outputs the same as above, but uses a font tag instead of span
// ( i dunno why you would do this but just in case you really want to go against good coding standards)
?>

Added on Jul 5th at 4 am by Scott - 0 Comments


0 Responses to gradient_text

Name:

Site or Mail:

Comment:

To try to reduce the ammount of spam, please enter the word GOOSE in the following form

Security Code: