Palindromes In PHP

1st June 2010

Richard Wiseman is a psychologist, magician, and author who runs a little blog over at http://richardwiseman.wordpress.com/. His blog talks about all sorts of things, but every Friday he posts a little puzzle that you can have a go at solving.

The last puzzle posted talked about palindromic numbers and speed, here is the puzzle in full.

The other day I went for a bike ride. My favourite route has signs every meter saying how far you have travelled. I came across the sign saying '15951 meters' and thought 'Oh, that's interesting, it is a number palindrome because it reads the same from left to right as right to left'. Then I rode on. Two hours later I came across the next palindromic number sign. How fast was I going?

I usually like to sit down for a few minutes and go over these puzzles in my head, especially if they are number based. However, this weeks puzzle got me thinking in terms of an algorithm that could work out the answer. All it would need is a counter for the start and finish, a simple palindromic detector, and a bit of maths at the end to work out the speed based on the start and end values.

  1. $start = 15951;
  2. $end = 0;
  3.  
  4. for ($i = $start + 1; $i < 20000; ++$i) {
  5. if ($i == strrev($i)) {
  6. print "Palindrome = " . $i . "\n";
  7. $end = $i;
  8. break;
  9. }
  10. }
  11. $result = $end - $start;
  12. // Work out speed.
  13. print $result . ' meters, or ' . $result / 1000 . 'km, travelled in 2 hours makes the speed ' . (($result) / 2) / 1000 . ' kph';

This prints out the following result.

  1. Palindrome = 16061
  2. 110 meters, or 0.11km, travelled in 2 hours makes the speed 0.055 kph

The answer to the Friday puzzle can be seen on Richard Wiseman's blog post from Monday. The answer to this puzzle was 55 meters per hour, which fits in with the answer this code created, although I worked it out in kilometres per hour.

Finally, if you are looking at string instead of a number you might need to improve the palindromic detector slightly by removing all punctuation and spaces.

  1. $string = preg_replace( '/[^\sa-zA-Z0-9]/', '', $string);
  2. if ($string == strrev($string)) {
  3. echo 'Panindrome detected.';
  4. }

It might also be worth using the strtolower() function to make sure that the string has no characters that might cause this palindrome check to fail.

Comments

Permalink
Using strrev() is not the proper way to do plaindrome. please provide a nice one to othres........

Submitted by Anonymous on Thu, 09/30/2010 - 12:46

Permalink

 

</p><p>$a  = "414";</p><p>$check = strlen($a);</p><p> </p><p>for($j=0; $j<$check; $j++)</p><p>{</p><p><span style="white-space: pre;">        </span>$fstr = $a[$j];</p><p><span style="white-space: pre;">   </span>$sec =  $check - $j-1;</p><p><span style="white-space: pre;">   </span>$lstr = $a[$check - $j-1];</p><p>if($fstr == $lstr)</p><p>{</p><p><span style="white-space: pre;">       </span>$flag = "ok";</p><p><span style="white-space: pre;">     </span>continue;</p><p>}</p><p>else</p><p>{</p><p><span style="white-space: pre;">      </span>$flag = "No";</p><p><span style="white-space: pre;">     </span>break; /* No need of further operation */</p><p>}</p><p><span style="white-space: pre;"> </span>}</p><p>echo "<br>";</p><p>if($flag == 'ok')</p><p>{</p><p><span style="white-space: pre;">      </span>echo $a." == this is palindrome number";</p><p>}</p><p><span style="white-space: pre;">  </span>else</p><p><span style="white-space: pre;">      </span>{</p><p><span style="white-space: pre;">         </span>echo $a."this is not a palindrome number";</p><p><span style="white-space: pre;">        </span>}</p><p>

 

Submitted by akhil raj srivasta on Tue, 03/13/2012 - 17:34

Permalink

 

</p><p>$str="1574751";</p><p>$ln=strlen($str);</p><p>for($i=$ln-1;$i>=0; $i--)</p><p>{</p><p><span style="white-space: pre;">   </span>$rstr[]=$str[$i];</p><p>}</p><p>$rstr=implode($rstr);</p><p>if($str==$rstr){</p><p><span style="white-space: pre;">      </span>echo $str." is palindrome.<br>";</p><p>}</p><p>else{</p><p><span style="white-space: pre;">      </span>echo $str." is not a palindrome.";</p><p>}</p><p>

 

Submitted by Shakti Shola on Mon, 03/26/2012 - 22:11

Permalink
this example is good to understand

Submitted by kuldeep sharma on Wed, 10/29/2014 - 10:40

Permalink
will you know how to find plaindrom in a string part, or a complete string? example: God saw I was a dog. Too bad - I hid a boot.

Submitted by rafael on Sun, 10/29/2017 - 04:54

Add new comment

The content of this field is kept private and will not be shown publicly.