You have to split the string up into an array. Here's what I got working:
Code:
function rainbow($str)
{
$array = str_split($str);
$i = $b = 0;
$str = '';
$colours = array('d31539', 'ff7e00', 'ffc20e', '90d125', '187acb', '6f3198', 'ab1d8e');
while( $i < count($array) )
{
if( $b > ( count( $colours ) - 1 ) ) $b = 0;
$str .= '<span style="color: #' . $colours[ $b ] . '">' . $array[$i] . '</span>';
++$b;
++$i;
}
return $str;
}