check if array text ends with an 's'

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
me666
Forum Commoner
Posts: 87
Joined: Wed Oct 08, 2008 5:04 pm

check if array text ends with an 's'

Post 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 :)
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

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

Post 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";
}
me666
Forum Commoner
Posts: 87
Joined: Wed Oct 08, 2008 5:04 pm

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

Post by me666 »

excellent, thank you very much.. this will help me solve serveral issues :)
Post Reply