I am using PHP and I have a backend form where the user must enter a letter using

120 views Asked by At

I have a backend form where the user must enter a letter using centurion letters such as ARESTU. I have already posted the word over to the PHP life and have used the is_numeric function to make user that the user enters a word and not aa number.

I know want to be able to convert the word to uppercase. I need to find the length of the word that the user has entered which I have already done using the strlen function. I know have to give each letter a value such as a = 1, b = 2, c = 3 etc.

I then need to check each letter in turn to see which letter the user has used from the letters they were given. If the user uses a letter that is not in the word then that letter is not given a value. I then want to be able to display display each letter of the word with the value.

Here is an example of what I want it to look like

If the user enters the word 'are'

  • A= 1
  • R= 18
  • E= 5

Total letter number is 24

Here is the code I have so far, this code only gets the word the user enters from the post, displays the word and shows the number of letters in the word.

<? php

$word = $_POST["word"];

$product = 1;


strtoupper($word);
print "$word <br> ";

if (is_numeric($word)) {
  print "Please enter a word";
}


$test = strlen($word);
print "Number of letters in the word are $test";

$lettersArray = array(
  'a' = 1,
  'e' = 5,
  'r' = 18,
  's' = 19,
  't' = 20,
  'u' = 21,
  'other' = 0,
);


$valueString = "";
for ($i = 0; $i < strlen($word); $i++) {
  $letter = strtolower(substr($string, $i, 1));
  $valueString. = $lettersArray[$letter];
}




$product = $product * $word;

?>
3

There are 3 answers

4
Dimag Kharab On BEST ANSWER

Her is your code with modification

EDIT 2

$word = "ARE";
$product = 1;
strtoupper($word);
print "$word <br> ";

if (is_numeric($word)) {
  print "Please enter a word";
}

$test = strlen($word);
print "Number of letters in the word are $test <br>";

$lettersArray = array(
  'a' => 1,
  'e' => 5,
  'r' => 18,
  's' => 19,
  't' => 20,
  'u' => 21,
  'other' => 0,
);

$valueString = 0;
$assignednumberalpha = null;

for ($i = 0; $i < strlen($word); $i++) {
  $letter = strtolower($word[$i]);
  $valueString = $valueString+$lettersArray[$letter];
  $assignednumberalpha .= "$letter =".$lettersArray[$letter].'<br>';;
}

$product = $product * $word;

echo $assignednumberalpha;
echo "The final Result : $valueString";

?>
0
Ashique C M On

Try this code:

<? php

$word = $_POST["word"];

$product = 1;


strtoupper($word);
print "$word <br> ";

if (is_numeric($word)) {
print "Please enter a word";
}


$test = strlen($word);
print "Number of letters in the word are $test";

// Your array
$lettersArray = array(
  'a' => 1,
  'e' => 5,
  'r' => 18,
  's' => 19,
  't' => 20,
  'u' => 21,
  'other' => 0,
);

// convert word into lower case or upper case
$word = strtolower($word);

// Split each chars of word and store in array
$chars = str_split($word);


// find the total in foreach or any other loop
foreach ($chars as $char)
{
   if(key_exists($char, $lettersArray))
      $product  = $product  *  $lettersArray[$char];
}

echo $product;
?>
1
anas On
$array = ['a' => 5, 'b' => 6, 'e' => 7, 'r' => 8, 't' => 9];
function myfunc($array, $word)
{
    $tmp = [];
    $word = strtolower($word);
    for ($i = 0; $i < strlen($word); $i++)
        $tmp[] = $array[$word[$i]];

    return array_sum($tmp);
}

print_r(myfunc($array, 'bat'));