Forum Moderators: coopster

Message Too Old, No Replies

remove part of string.

         

hypnotic monkey

11:07 am on Jan 14, 2010 (gmt 0)

10+ Year Member



i have item IDs (VA10086, VA10087 etc)

in the database they are 86,87 etc. so did:

$ID = $result['ID'] + 10000
echo "VA".$ID

my question is how can i get it back to just 86,87 from a search form to get a query i want?

i want item.php?id=VA10086
but to get database it needs to be
select * from database where id=86

how do i remove the VA bit? cos i can then do -10000

cheers.

Frank_Rizzo

11:31 am on Jan 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the string is a constant size and your ID no never goes above 9999:

$ID = 'VA10089';
$id_no = 1 * substr($ID, - 4);

This will extract the last 4 digits. The 1 * ... part removes the front zeroes if they exist to just return

89

-------------------

If the string is variable size such as

VA1025
VA100020087

then the above will not work. In this case if the VA1 is consistent (i.e. you don't have VB10002, XY80023) then just replace that string with blank

$id_no = 1 * str_replace('VA1', '', $ID);

or if VA1 is not consistent in character but is in length (i.e. 2 letters, 1 character VB2 XY7) then try this:

$id_no = 1 * substr($ID, 3);

This will extract the part of the string after the first 3 chars.

hypnotic monkey

12:17 pm on Jan 14, 2010 (gmt 0)

10+ Year Member



the VA is constant - the rest are autoinc in the database + 10000.

so would this be correct?:
$ID = "VA10103";
$id_no = 1 * str_replace('VA', '', $ID);
$id_fin = $id_no - 10000;

i will test it when i get a chance this eve :)

thanks!