/    Sign up×
Community /Pin to ProfileBookmark

php code for palindrom

php script is new for me, can anyone help me about this palindrome thing in php… please please please…

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@jhamskee17authorAug 27.2006 — i'm just a beginner in php. can any one help me with this palindrome thing using php script... thank you!
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYAug 27.2006 — which palindrome thing?!
Copy linkTweet thisAlerts:
@pcthugAug 27.2006 — Exactly, which palindrome thing?! To me a palindrome is any Word, string, etc. that is exactly the same read in either direction. Nothing to do with PHP
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYAug 28.2006 — well "PHP" is a palindrome :p ?
Copy linkTweet thisAlerts:
@pcthugAug 28.2006 — well "PHP" is a palindrome :p ?[/QUOTE]
hahaha ? ? ?
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYAug 28.2006 — hi LiLcRaZyFuZzY... i have this assignment from my professor, i dont know how to do it... it states that

"Make a program using any control structure statement that validate if

a word type by a user is a Palindrome or not. ( words when read backwards

will be the same ) . example aha, madam, eye

output :

Enter a word :

aha

aha is a palindrome !!!

Enter a word :

make

make is not a Palindrome ."

PLEASE HELP ME...[/QUOTE]



post here if you have questions about the following code:

[code=php]
<?php
function is_palindrome($string){
if(!empty($string) && isset($string) && is_string($string)){
if(strlen($string) > 2){
if(strlen($string) % 2 == 1){
# string has a center
$strlen_first_half = floor(strlen($string) / 2);

$center = $strlen_first_half; # strings start counting at 0

$str_first_half = substr($string, 0, $strlen_first_half);
$str_second_half = substr($string, -$strlen_first_half);

$str_center = $string{$center};

if(strrev($str_second_half) == $str_first_half){
echo "<span style='font-weight: bold;'>'$string'</span> is a palindrome<br>";
echo "> ".$str_first_half."<span style='color: red; font-weight:bold;'>".$str_center."</span>".$str_second_half;
}else{
echo "<span style='font-weight: bold;'>'$string'</span> is not a palindrome<br>";
}
}else{
# string is dividable by 2
$strlen_first_half = strlen($string) / 2;

$str_first_half = substr($string, 0, $strlen_first_half);
$str_second_half = substr($string, -$strlen_first_half);

if(strrev($str_second_half) == $str_first_half){
echo "<span style='font-weight: bold;'>'$string'</span> is a palindrome (With an even string length and no central letter)<br>";
echo "> ".$str_first_half."|".$str_second_half;
}else{
echo "<span style='font-weight: bold;'>'$string'</span> is not a palindrome<br>";
}
}
}else{
die("String length must be higher than 2");
}
}else{
die("Enter a string");
}
}

is_palindrome("madam");
?>
[/code]
Copy linkTweet thisAlerts:
@sridhar_423Aug 28.2006 — [code=php]function is_palindrome($string){
if(!empty($string) && isset($string) && is_string($string)){
if(strrev($string) == $string)
echo "Yes.. Its Palindrome";
else
echo "No.. not Palindrome";
}
[/code]
Copy linkTweet thisAlerts:
@sridhar_423Aug 28.2006 — Sorry.. missed out a closing brace
[code=php]function is_palindrome($string){
if(!empty($string) && isset($string) && is_string($string)){
if(strrev($string) == $string)
echo "Yes.. Its Palindrome";
else
echo "No.. not Palindrome";
}
}[/code]
×

Success!

Help @jhamskee17 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...