Trim and substr help

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
dimxasnewfrozen
Forum Commoner
Posts: 84
Joined: Fri Oct 30, 2009 1:21 pm

Trim and substr help

Post by dimxasnewfrozen »

I have an ID field that will start with a letter and may have any number of 0's after the letter.

Example:
E00892911 -> 892911
E012993 -> 12993
E178239 -> 178239
L00002134 -> 2134

I need to trim the first letter, which I know how to do:

Code: Select all

$my_new_id = trim($my_id, $my_id[0]);
Now, I need to remove all the 0's in the beginning of $my_new_id, there's no defined amount of how many occurrences there will be. There could be none, or there could be 8.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Trim and substr help

Post by infolock »

Just do a regular expression match with preg_match. You could also split it, but it's up to you on how you want to handle it.

Could also just run intval or typecast it as a (int) it a basic integer... but again, depends on what you want to do...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Trim and substr help

Post by Weirdan »

dimxasnewfrozen wrote: Now, I need to remove all the 0's in the beginning of $my_new_id, there's no defined amount of how many occurrences there will be. There could be none, or there could be 8.
You can do that with trim as well:

Code: Select all

$my_new_id = ltrim($my_id, $my_id[0] . "0");
Post Reply