Ok! I have this script but I'm puzzled! .. I assigned each letter (a, b ,c ...etc.) to a number .. so whenever I write a word (cake) it should write the numbers .. but how do I make it use the "+" function to give me the total result?

here is a slice of the code ::

<?PHP
include("header.inc")
?>
<?
for ($x = 0; $x < strlen($userString); $x++)
{
$thischar = substr($userString, $x, 1,);

if ($thischar == "a)
{echo ("1);}
else
if ($thischar == "b)
{echo ("2);}
else
print ("(ERROR)");
}

then it prints the resulted numbers in a textarea field...
How can I make the script do the function "+" to a & b so that ab=3 as assigned in the script?

Thank you!

    Ok I added this ::

    if ($thischar == " ")
    {$total = $x + $x;}
    else

    now the problem is >> a a a = 6.. not 3 !!!!!

    Help please!

      You can use the ord() function ๐Ÿ™‚
      Every character is represented by a number.
      For example "a" is 97, "b" is 98 and so on...
      Note: "A" is 65 ... NOT 97!

      so you can use this function instead of the IFs
      have a look at this:

      $sum = 0;
      $ordOfA = ord("a");
      for ($x=0; $x<strlen($userString); $x++) {
        $thischar = substr($userString, $x, 1);
        $sum += ord($thischar)-$ordOfA+1;
      }
      

      at the end... $sum will have the addition you are looking for ๐Ÿ˜‰

      all the above will work good assuming that the user enters only lower case letters because as I said.. ord of a is 97 but ord of A is 65
      so perhaps you might want to lower case the input string first, then work out the numbers

        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

          Ok...
          first of all you can use asci values to get numeric values for letters.

          it goes from capital A = 65 to capital Z = 90
          small a = 95 to small z = 122;

          if you want to ignore case you can use 'strtoupper';

          if you want A to be 1 you subtract 64

          so here is a function that will take a string and output
          a string made up of the numbers correponding to each letter:

          function convertString($string){
          	$string = strtoupper($string);
          	$output = "";
          	for ($i = 0; $i < strlen($string); $i++) {
          		$output .= ord($string{$i}) - 64;
          	}
          	return $output;
          }
          

          so echo convertString('cake') would output 31115.

          you might want to add a dilimiter:
          $output .= ord($string{$i}) - 64 . " ";

          here is a function that would add the numbers:

          function addStringNumbers($string){
          	$string = strtoupper($string);
          	$output = 0;
          	for ($i = 0; $i < strlen($string); $i++) {
          		$output += ord($string{$i}) - 64;
          	}
          	return $output;
          }
          

          here echo addStringNumbers('cake') would output 20.

            Hey! .. umm I tryed the long code thing and it worked but only till 10 ...

            See the problem is I'm not using english letters in my project I just put them for easy example preview ... and the method I was using is as follows ::

            ร‡=1
            รˆ=2
            รŒ=3
            ... till 10, then ::
            รŸ=20
            รก=30
            ...till 100, then it goes till 900, then the last letter is assigned to 1000.

            So as you can see each way won't work :/

            But thanks for your help guys!

              Ok,
              in your code you use:
              for ($i=0; $i < count($alphabet); $i++) {
              to loop through the alphabet and use ($i + 1) as the value for each letter.

              If I understand you, the eleventh letter should have a value of 20, the 12th should be 30 etc.. ($i + 1) in your code would be give 11 and 12, respectively.

              I think if you drop this code into yours, it will give you the result you are looking for:

              $exp=0;
              $j=0;
              for ($i=0; $i < count($alphabet); $i++) {
              	if ($j++ < 9) {
              		$exp++;
              		$j=2;
              	}
              	if ($thischar[$k] == $alphabet[$i]) {
              		$sum = $sum + ($j * pow(10,$exp));
              	}
              }
              

              although it would work faster if you would create an associative array first and then used it to find the values for each letter:

              $exp=0;
              $j=0;
              for ($i=0; $i < count($alphabet); $i++) {
              	if ($j++ > 9) {
              		$exp++;
              		$j=2;
              	}
              	$alph2val[$alphabet[$i]] = $j * pow(10,$exp);
              }
              
              for ($k=0; $k < strlen($user_string); $k++) {
              	if (isset($alph2val[$user_string{$k}])) $sum += $alph2val[$user_string{$k}];
              }
              
                Write a Reply...