Page 1 of 1

Trim and substr help

Posted: Thu Sep 02, 2010 11:27 am
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.

Re: Trim and substr help

Posted: Thu Sep 02, 2010 1:26 pm
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...

Re: Trim and substr help

Posted: Thu Sep 02, 2010 3:25 pm
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");