Standard Palindrome in PHP

A Standard Palindrome is a word or phrase that is the same forward as it is backwards without punctuation and spaces. So here is a small PHP script that will check if a string is a standard palindrome.

Replace the value you want of $originalPalindromeToTest with the string you want to check is a palindrome or not.

This PHP script will strip all of the spaces and reverse the order of the string to check if it’s a palindrome (as you can see in the if statement below).

<?php
$originalPalindromeToTest = "Madam I'm Adam";
$palindrome = strtolower(preg_replace("([^A-Za-z0-9])", "", $originalPalindromeToTest ));
$palindromeReversed = strtolower(strrev($palindrome));
echo "Original Statement: $originalPalindromeToTest <br /><br />";

if ($palindrome == $palindromeReversed){
echo "$palindrome is a Standard Palindrome of $palindromeReversed";
}else{
echo "$palindrome is <strong>not</strong> a Standard Palindrome of $palindromeReversed";
};
?>

1 thought on “Standard Palindrome in PHP”

  1. You should work on clean code. If you have palindrome2, the other variable should be palindrome1.

    Also, use white space. Indenting is good too for legibility.

    Reply

Leave a Comment