Skip to content

Instantly share code, notes, and snippets.

@iamshanto
Last active December 16, 2015 14:49
Show Gist options
  • Save iamshanto/5451899 to your computer and use it in GitHub Desktop.
Save iamshanto/5451899 to your computer and use it in GitHub Desktop.
<?php
function printPalindromes($str)
{
#$str = strtolower(preg_replace("'\s+'", '', $str));
#$str = strtolower(preg_replace("([^A-Za-z0-9])", "", $str));
$palindrom = $str;
$str1 = strtolower($str);
$matches = array();
$str = $str1;
for ($i = 0; $i < strlen($str); $i++) {
$str2 = substr($str, $i);
for ($j = 0; $j < strlen($str2); $j++) {
$tmp = substr($str2, 0, $j+1);
if( $tmp == strrev($tmp)){
$matches[] = $tmp;
}
}
if( $str == strrev($str)){
$matches[] = $str;
}
}
$matches = array_unique(array_filter($matches));
$found = count($matches);
if ($found == 0) {
echo 'No Palindrom found';
} else {
echo 'The Word "'.$palindrom.'" contains '.$found.' palindromes:' . '<br>';
foreach ($matches as $key => $match) {
echo $match . '<br>';
}
}
}
printPalindromes('madam');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment