Checking if a value is a palindrome, need help please!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Checking if a value is a palindrome, need help please!

Post by mikeashfield »

Code: Select all

<?php
if (isset($_POST['palindrome']) && strlen($_POST['palindrome']) > 1) {
    //A value has been passed and is longer than 2 characters.
    $check=$_POST['palindrome'];
    if ($check === strrev($check)) {
        //The value passed is a palindrome
        echo $check." is a palindrome!";
    }
    elseif ($check !== strrev($check)) {
        //The value passed is not a palindrome
        echo $check." is not a palindrome!";    
    }
}
else {
    //An invalid value was passed.
    echo "An invalid value was passed, sorry.";
} 
?>
Okay, I'm new to PHP and as such have been messing about, trying to learn some of the functions ect and have been getting the outputs I expect, but when I help (try to, anyway!) the code post is for some reason or another, no good. Could anybody tell me if there's anything wrong with the above code, as I get the outputs I expect. I'm still learning, so any criticism is valued. Thanks.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Checking if a value is a palindrome, need help please!

Post by Celauran »

A string of length 1 will always be a palindrome.
maxx99
Forum Contributor
Posts: 142
Joined: Mon Nov 21, 2011 3:40 am

Re: Checking if a value is a palindrome, need help please!

Post by maxx99 »

Not much to do wrong here :)
...but as far as I remember (correct me if im wrong) "strrev" is compatible only with single byte character-sets :x it will fail e.g. UTF-8 chars
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: Checking if a value is a palindrome, need help please!

Post by mikeashfield »

That's why I'm not allowing it. :) But thanks for the heads up.
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: Checking if a value is a palindrome, need help please!

Post by mikeashfield »

maxx99 wrote:Not much to do wrong here :)
...but as far as I remember (correct me if im wrong) "strrev" is compatible only with single byte character-sets :x it will fail e.g. UTF-8 chars
Believe me, I've been getting it wrong, all of it, apparently. :lol:

So what would be the alternative then?
maxx99
Forum Contributor
Posts: 142
Joined: Mon Nov 21, 2011 3:40 am

Re: Checking if a value is a palindrome, need help please!

Post by maxx99 »

There is a number of problems and solutions related to this function :)
Check them out in comment section on http://php.net/manual/en/function.strrev.php
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Checking if a value is a palindrome, need help please!

Post by Benjamin »

Don't know if this will work, but figured I would post it anyway:

Code: Select all

if ($n == implode('', array_reverse(str_split($n)))) {
    echo 'yes';
} else {
    echo 'no';
}
maxx99
Forum Contributor
Posts: 142
Joined: Mon Nov 21, 2011 3:40 am

Re: Checking if a value is a palindrome, need help please!

Post by maxx99 »

Both will fail for utf8 encoded string e.g.

Code: Select all

$n = utf8_encode("002 €ąż źź żą€ 200");
but will work for

Code: Select all

 $n = "002 €ąż źź żą€ 200"; 
:x
Post Reply