Return to Snippet

Revision: 69868
at September 25, 2015 14:27 by boloround


Initial Code
<?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
    $string = strtolower($string);
    // Uses regular expression to replace all non-word characters such as puncuation and whitespace with nothing
  	$forward_string = preg_replace("/\W/", "", $string);
    // Reverses the above string and sets value to new variable
  	$backward_string = strrev($forward_string);

        // 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/
?>

Initial URL


Initial Description
Script checks whether a given string is a palindrome

Initial Title
Palindrome checker

Initial Tags


Initial Language
PHP