Skip to content

Instantly share code, notes, and snippets.

@devd123
Created April 24, 2017 05:58
Show Gist options
  • Save devd123/faf1a4fd4c03e936a59bd4945b663413 to your computer and use it in GitHub Desktop.
Save devd123/faf1a4fd4c03e936a59bd4945b663413 to your computer and use it in GitHub Desktop.
Find longest palindrome string in php
<?php
/**
* find the longest palindrome in a string
* website : neerusite.wordpress.com
* Auther : Devdutt Sharma
*/
function longestPalindrome($string = ''){
$strArr = array();
for($i=0; $i<strlen($string); $i++ )
{
$palindrome = true;
$offset = 1;
while($palindrome)
{
$word = substr($string, $i-$offset, ($offset*2)+1 );
if( $word == strrev($word) ) {
//print strlen($word) .' '. $word.'<br />';
$strArr[$word] = strlen($word);
} else {
$palindrome = false;
}
$offset++;
}
}
$finArr = max($strArr);
echo $key = array_search ($finArr, $strArr);
}
longestPalindrome($string = "abcdcbadfddeegfdsrefgr");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment