Write a PHP program to find a string is palindrome or not

A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam or racecar. There are also numeric palindromes, including date/time stamps using short digits 11/11/11 11:11 and long digits 02/02/2020. Sentence-length palindromes ignore capitalization, punctuation, and word boundaries.


<?php

function check_plaindrome($string) {

    //remove all spaces

    $string = str_replace(' ', '', $string);

    //remove special characters

    $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string);

    //change case to lower

    $string = strtolower($string);

    //reverse the string

    $reverse = strrev($string);

    if ($string == $reverse) {

        echo "<p>It is Palindrome</p>";

    } else {

        echo "</p>Not Palindrome</p>";

 }

}

$string = "madam";

check_plaindrome($string);

Output - 

It is Palindrome

No comments:

Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...