Removing certain num of characters from end of 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
opengavel
Forum Newbie
Posts: 10
Joined: Tue Sep 12, 2006 2:06 pm
Location: Chicago

Removing certain num of characters from end of string

Post by opengavel »

I can't seem to find a simple way to separate or remove a certain number of characters from the end of a string

I have a group of id numbers (e.g., F001, NY230, IL303, etc.) that are either 4 or 5 characters long. I need to separate the last 3 numerical characters from the remaining 1 or 2 alpha characters. Is there an easy way to trim a string by the location of the characters or by the numeral/alpha difference?

I hunted around for a string function but none seem designed to do this.

Any advice on the shortest and simplist way to accomplish this?

Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

opengavel
Forum Newbie
Posts: 10
Joined: Tue Sep 12, 2006 2:06 pm
Location: Chicago

Post by opengavel »

Yikes, I missed that one.

Thanks.
opengavel
Forum Newbie
Posts: 10
Joined: Tue Sep 12, 2006 2:06 pm
Location: Chicago

Post by opengavel »

I got it to work with fine with a combination of substr (to get the last 3 chars) and trim (to remove those char, getting the remaining 1 or 2).

Thanks again.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

trim() shouldn't be needed.

substr($string, 0, -3)
Post Reply