determining the last character of a string?

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
psychomachine
Forum Newbie
Posts: 17
Joined: Wed Mar 03, 2004 1:18 pm

determining the last character of a string?

Post by psychomachine »

hello there.

i have a mysql query that returns a string and puts it in a variable

$string

I need to check if the last character of $string is "a", "b" or "c"; and if it is, create a new variable

$newstring

which will be the same as $string, except "a" will be replaced by "d", "b" will be replaced by "e", and "c" will be replaced by "f".

many thanks in advance.

psychostuck
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post by Deemo »

substr()

use that, and another idea would be to use strrev, and then read the last letter, which is now the first letter :D
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

a example if u want:

Code: Select all

<?php
$exam = substr("evilwalrus", -1);    

?>
this will return the last letter, "s" in this case.
psychomachine
Forum Newbie
Posts: 17
Joined: Wed Mar 03, 2004 1:18 pm

Post by psychomachine »

sorry, i forgot to mention the most important thing!!!! the strings from the database are utf8 encoded cyrillic characters. so each character is actually two bytes, which is why the above suggestions don't work. how does one make sure that the last character is actually the last unicode character?

many thanks, again.
psychomachine
Forum Newbie
Posts: 17
Joined: Wed Mar 03, 2004 1:18 pm

Post by psychomachine »

ok, figured it out. the solution is to use mb functions, but if you can't, simply count each character as two. it works.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

would you mind posting the code so I can see it? curious =]
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

[php_man]mb_substr[/php_man] I guess :D
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Alternatively you can treat a string as an array. Meaning:

Code: Select all

$string="Hello"l
echo $string[4];
should output "o"

Code: Select all

echo $string[strlen($string)-2];
should output the last character of a string. Note: -2 since arrays are indexed starting with 0, while the strlen counter starts with 1.

Edit: ack, too tired. Just read your message regarding unicode - the code above can be modified for that purpose.
psychomachine
Forum Newbie
Posts: 17
Joined: Wed Mar 03, 2004 1:18 pm

Post by psychomachine »

tim said:
would you mind posting the code so I can see it? curious =]
sure. without using mbstring, php thinks that each byte is one character. so, in order to get one unicode character, we are telling php to get two character bytes.

$lastletter = substr($string, -2);

and to get the word without the last letter:

$allbutlast= substr($string, 0 (strlen($string)-2));

tested and works with utf8 cyrillic from my db.

all best,

psychomachine
Post Reply