In: Computer Science
Write a PHP script that checks a word or a phrase (stored in a string variable) to determine if it is a standard palindrome, a perfect palindrome, or not a palindrome at all. Also, for each letter that the word/phrase contains, count and print the number of times that each consonant and vowel is encountered. Your output will look as follows:
Word/phrase: racecar
Perfect palindrome
Contains consonants:
r - 2
c - 2
Contains vowels:
a - 2
e - 1
<?php
$word = "racecar";
echo "Word/phrase: $word<br>";
$reversed_word = strrev($word);
// converting to lower character after replacing special char
$refined_word = strtolower(preg_replace("([^A-Za-z0-9])", "", $word
));
$reversed = strtolower(strrev($refined_word));
if ($word === $reversed_word)
{
echo "Perfect Palindrome<br>";
}
else{
if ($refined_word == $reversed){
echo "Standard
Palindrome<br>";
}
else{
echo "Not a
palindrome<br>";
}
}
$vowel = array('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U');
echo "Contains consonants:<br>";
foreach (count_chars($word, 1) as $strr => $value) {
if (ctype_alpha(chr($strr)) &&(!
in_array(chr($strr), $vowel))) {
echo chr($strr) . " - $value
<br>";
}
}
echo "Contains vowels:<br>";
foreach (count_chars($word, 1) as $strr => $value) {
if (ctype_alpha(chr($strr))
&&(in_array(chr($strr), $vowel))) {
echo chr($strr) . " - $value
<br>";
}
}