Page 1 of 1

check if array text ends with an 's'

Posted: Mon Feb 15, 2010 3:07 pm
by me666
hi,
i have some code to take the name of a user from a database and print it on screen, all works fine but i am looking to put 's at the end, so the name could be joe and would display as joe's... i can also do that, but if the name ends with s i would like to put just a ' at the end rather than 's
i have tried using an if statement like this...

Code: Select all

<php
if($name == %s){
print"$name'";
}
else{
print"$name's";
}
?>
but this returns an error stating there is an unexpected % on line2
how could i change this to do as i'm looking to?
thanks for the help :)

Re: check if array text ends with an 's'

Posted: Mon Feb 15, 2010 3:09 pm
by Darhazer
string is an array of character, so you have to check if the last character is s:

Code: Select all

if ($name[ strlen($name)-1 ] == 's') {
echo $name . "'";
} else {
echo $name ."'s";
}

Re: check if array text ends with an 's'

Posted: Mon Feb 15, 2010 3:27 pm
by me666
excellent, thank you very much.. this will help me solve serveral issues :)