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

Popular posts from this blog

Data Modeling

Organization Tips for Freshers

Relational DB, ORM and ERP systems

Data Engineering Tools

Applications of Data Engineering

Relational Data Model

Probability And Statistics For Engineers

Good Pieces of Advice for Freshers

Simple Web Page : Step by Step

Github : Part 2 : Final : Full Guide