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

1-Machine Learning : Start...

Data Engineering Tools

A Survey of the Final Year University Students

Five People to Follow on Social Media

How to take notes in Freshman Year ?

E-Commerce : Platforms

Web Applications

Data Analysis : Reading Habit Analysis

Web Services : AWS (Amazon Web Services)

Cloud Computing