/ Published in: PHP
Script checks whether a given string is a palindrome
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php // Declares variables I'll use later $forward_string = ""; $backward_string = ""; // Function to compare two strings, takes a string for argument function strip_punctuation($string) { // Sets entire string to lower case // Uses regular expression to replace all non-word characters such as puncuation and whitespace with nothing // Reverses the above string and sets value to new variable // Conditional to compare the two strings. If they are equal, echo out "Palindrome", else if not, echo "Not a palindrome" if ( $forward_string == $backward_string ) { echo "Palindrome " . $forward_string; } else { echo "Not a palindrome " . $backward_string; } } // Call the function strip_punctuation("Was it a car or a cat I saw?"); // Original code challenge at: https://www.reddit.com/r/dailyprogrammer/comments/3kx6oh/20150914_challenge_232_easy_palindromes/ ?>