Palindrome : php
Image source : https://www.grammarly.com/blog/16-surprisingly-funny-palindromes/ |
Words, numbers, digits, or ... anything, that remain the same as original if we reverse them are Palindromes.So how could we find if a word or a sentence is palindrome or not? We will use php for this...
code:
<?php function semiPermeableMembrain($str){ $str = strtolower($str); $split = str_split($str); foreach($split as $obstacle){ if($obstacle == " " || $obstacle == "'" || $obstacle == "." || $obstacle == "," || $obstacle == ";" || $obstacle == ":" || $obstacle == "-" ) { $obstacle = "/$obstacle/"; $str = preg_replace($obstacle,'',$str); } } palindrome($str); } function palindrome($str){ // wrote that in the above function for simplicity :) //$str = preg_replace('/\s+/', '', $str); //to remove spaces //$str = preg_replace('/\'/', '', $str); //to remove ' //$str = preg_replace('/\./','', $str); //to remove . //$str = preg_replace('/\,/','', $str); //to remove , $rev = strrev($str); if($rev == $str){ echo "<i>".$str."</i>"." : yes a Palindrome :) <br>"; } else{ echo "<i>".$str."</i>".": not a Palindrome :( <br>"; } } semiPermeableMembrain("Madam"); echo "<br>"; semiPermeableMembrain("A man, a plan, a canal -- Panama"); echo "<br>"; semiPermeableMembrain("Madam I'm Adam"); echo "<br>"; semiPermeableMembrain("Able I, able"); echo "<br>"; semiPermeableMembrain("dad");?>
OUTPUT:
Comments
Post a Comment