Damn PHP functions! I spent a while playing around with this to find out there's already a function that does this. ๐
Anyways, here's what I came up with. Altexis' method is MUCH shorter and easier, but if I don't post what I came up with, I won't be happy . . .
<?
// create array from alphabet
$alphabet = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
// user string
$user_string = "CaKe";
// convert that string to lowercase
$user_string = strtolower($user_string);
// define starting sum
$sum = "0";
// define starting value for $thischar
$thischar = "";
// get each letter seperate then add \n at end so we can layer convert to array
for ($x=0; $x < strlen($user_string); $x++) {
$thischar .= substr($user_string, $x, 1) . "\n";
}
// put in array
$thischar = explode("\n", $thischar);
// check array $thischar against array $alphabet.
// if the particular letter matches a letter in the alphabet
// create a new sum
for ($k=0; $k < (count($thischar) - 1); $k++) {
for ($i=0; $i < count($alphabet); $i++) {
if ($thischar[$k] == $alphabet[$i]) {
$sum = $sum + ($i + 1);
}
}
}
// print sum
echo "Sum: " . $sum;
?>
Cgraz