Created
October 19, 2013 06:13
-
-
Save guywarner/7052190 to your computer and use it in GitHub Desktop.
Method to take a string and print all palindromes. Returns the longest or if none found throws an error.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function palindrome($string) { | |
$string = strtolower(ereg_replace("[^A-Za-z0-9]", "", $string )); | |
$palindromes = array(); | |
$longest = ""; | |
$word = ""; | |
$length = strlen($string); | |
for ($i =0; $i <= $length; $i++) { | |
$word = $string[$i]; | |
for ($j = $i + 1; $j <= $length - 1; $j++) { | |
$word .= $string[$j]; | |
if ($word == strrev($word)) { | |
print ("$word <br/>"); | |
if (strlen($word) > strlen($longest)) { | |
$longest = $word; | |
} | |
} | |
} | |
$word = ""; | |
} | |
if ($longest != "") { | |
return $longest; | |
} else { | |
throw new Exception("Error no palindrome's in given string"); | |
} | |
} | |
$string = "A Toyota's a Toyota. ajdskafhdasjfh ak asdf asfd | |
Able was I ere I saw Elba. asfasf saf afdasfa fkdkre | |
Acrobats stab orca. asldkfjrndd | |
Aerate pet area. afkdsfljnreng | |
Ah, Satan sees Natasha! afskanglgele | |
Aibohphobia (fear of palindromes) | |
Air an aria. asfsafasfas aaf a fds | |
Al lets Della call Ed Stella. asfa gfafa dsf afd | |
alula asfafsad fdf | |
Amen icy cinema. afd adf dsafsafd | |
Amore, Roma. safasaf d | |
Amy, must I jujitsu my ma? asf sadfdsf | |
Anaa asdfa sd fa sf | |
Animal loots foliated detail of stool lamina. | |
Anna sfsdfds fsdf | |
Anne, I vote more cars race Rome to Vienna. | |
Are Mac ‘n’ Oliver ever evil on camera? | |
Are we not drawn onward to new era? | |
Are we not drawn onward, we few, drawn onward to new era? | |
r gsdgfsdfg Are we not pure? “No sir!” Panama’s moody Noriega brags. “It is garbage!” Irony dooms a man; a prisoner up to new era. sdgdfsg sf sf | |
Art, name no tub time. Emit but one mantra. asf fdsa ff safsaf | |
As I pee, sir, I see Pisa! asfsffawefef efggthlfdlkd | |
Avid diva."; | |
echo "Org: $string<br/>"; | |
try { | |
$longest = palindrome($string); | |
} catch (Exception $e) { | |
echo "error:" . $e->getMessage(); | |
} | |
echo "Longest: $longest"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment