Click to See Complete Forum and Search --> : PROBLEM... with String and Integer!


lting
01-13-2004, 01:15 AM
i have an employee ID in mysql database as

M001 - in mysql database it appears as VARCHAR ..


now i want to extract M from the M001 using php commands ..
any idea how?

and what if i have two Employee IDs with
M001 and SW001 ..

how i extracts the string away from interger?

Khalid Ali
01-13-2004, 07:35 AM
go thru the whole string, find i there are any chars and use
substr functions,
View the documentation at
php.net
They have a list of string manipulation functions.

pyro
01-13-2004, 08:58 AM
The easiest way might be to use a regex to remove all non-numeric characters:

<?PHP
$str = "M001";
$num = preg_replace("/\\D/", "", $str);
echo $num;
?>

lting
01-13-2004, 05:56 PM
emm

emm then after i took out the non-numeric character

how i join it back together?

YoN
01-13-2004, 06:03 PM
In the example that pyro gave you $str keeps the original value (The one with letters and numbers) and $num has the new value with only numbers. :)

lting
01-13-2004, 09:05 PM
emm yupp

becos if M001 is the original value ..

then i want to extract the M from it ..

and +1 to the interger Value ..

then join them back to become

M002 ...

or any other ways to do this?

e.g M001, i want to add 5 to it, then it become M006

pyro
01-13-2004, 09:09 PM
The answer is going to require a bit more info. It would be quite simple to write something that works for the situation you just described, but how portable must this be? What are a few other examples?

lting
01-13-2004, 09:37 PM
ops ..

cos i want to do a button which will auto generate the next ID for me ..

like now is M001 when the button is clicked it become M002 ..

if the ID start with SW001 when the button is clicked it become SW002, if start with SW011 when the button is clicked it becomes SW012...

so i hope can extract the alphabert from the values ..

then i plus 1 for the integer .. then join them back together ..

this way look stupid .. but i really no idea how to do the Get Next ID button ...

pyro
01-13-2004, 10:47 PM
This should get you close then. If you have any questions (how it works, or otherwise) let us know.

<?PHP
$str = "M001";
list($alpha, $num) = preg_split("/(?<=[a-z])(?=\\d)/i", $str);
$len = strlen($num);
$num++;
$str = $alpha.str_pad($num, $len, "0", STR_PAD_LEFT);
echo $str;
?>

lting
01-13-2004, 11:43 PM
thanks ..
pyro ..

it works what i really want it to do ..

emm another question .. ( as just wish to know )

what if my Id is M02S01.. when i click next ID it becomes M02S01. .. how you take the alphaberts out and put them together agian .. cos you don't to know which character is start with alphaberts or numeric ?

lting
01-14-2004, 01:35 AM
ops problem again pyro ..

i get the values from the database ..

so if the value is M001 .. it works fine!

but if the value taken from database is only numeric number..
problem occurs ..

:(

what to do? in database the ID type is VARCHAR ...

vertigo_one
01-14-2004, 09:14 AM
I think you should check if it's there any string, if it is, do the stuff above, if not, just return the number.
(Current Code hasn't been tested, use this under your own risk :p, j/k)

<?PHP

$str = "M001";

list($alpha, $num) = preg_split("/(?<=[a-z])(?=\d)/i", $str);

$len = strlen($num);

$num++;

if(strlen($alpha) == 0)
{
$str = $num;
}
else
{
$str = $alpha.str_pad($num, $len, "0", STR_PAD_LEFT);
}

echo $str;

?>


(Code above copyrighted by pyro :p)

pyro
01-14-2004, 09:28 AM
Actually, the best way would be to simply change the regex. Had I known of the above two scenarios in the first place (which is why I asked) it would have looked more like this:

<?PHP
$str = "001";
//$str = "M02S01";
list($alpha, $num) = preg_split("/(?=\\d$)/i", $str);
$len = strlen($num);
$num++;
$str = $alpha.str_pad($num, $len, "0", STR_PAD_LEFT);
echo $str;
?>

vertigo_one
01-14-2004, 09:51 AM
I sux!

lting
01-16-2004, 12:18 AM
i try the code, it works fine from 001 to 009, but when reach 009, the next id become 0010, can't it be like 010?

pyro
01-16-2004, 07:30 AM
Alright, let's do it like this, then:

<?PHP
$str = "009";
//$str = "M02S01";
preg_match("/(.*?)(\\d*)$/", $str, $matches);
$alpha = $matches[1];
$num = $matches[2];
$len = strlen($num);
$num++;
$str = $alpha.str_pad($num, $len, "0", STR_PAD_LEFT);
echo $str;
?>

vertigo_one
01-16-2004, 07:30 AM
maybe just try swapping

$len = strlen($num);
$num++;

by

$num++;
$len = strlen($num);

hope it works

bye