Jump to content

[SOLVED] strings help


digitalgod

Recommended Posts

are you certain that the last 4 digits will always be numeric?

 

if so, you can try this:

<?php
    $old_code = 'WLK0001';
    $new_code = substr($old_code, 0, 3) . (substr($old_code, -4) + 1);

    echo $new_code;
?>

 

 

it might be better for you to throw in a is_numeric() or, better yet, ctype_digit() function in there too.

Link to comment
https://forums.phpfreaks.com/topic/49534-solved-strings-help/#findComment-242812
Share on other sites

i'm sorry, i just realized i've made a mistake. since the code i posted is adding a decimal number, you will get WLK2

 

 

what you could do is use str_pad()

<?php
    $old_code = 'WLK0001';
    $added_num = substr($old_code, -4) + 1;
    $new_code = substr($old_code, 0, 3) . str_pad($added_num, 4, '0', STR_PAD_LEFT);

    echo $new_code;
?>

 

 

but this is also not tested so try it out and let me know.

Link to comment
https://forums.phpfreaks.com/topic/49534-solved-strings-help/#findComment-242822
Share on other sites

Is there any reason you don't break it into two fields?

 

You can always concatenate them as you pull from the database and pad the number with leading zeros for numbers less than 1000.

 

well it's a PO number from an invoice, there are 3 types of invoices and each type starts with it's own 3 letters followed by the digits..

 

Thanks Koobi it works perfectly

Link to comment
https://forums.phpfreaks.com/topic/49534-solved-strings-help/#findComment-242837
Share on other sites

In our software we have what's called an option number, which is really a concatenation of a trade code and an actual number.

 

In your SQL you can do:

 

SELECT CONCAT( field1, LPAD( field2 ) ) AS PONum FROM ...

 

To pull the data in the format you wish to use it.  But separating the fields will give you advantages down the road when you wish to sort  ;)

 

Of course, you can work with the system you have an all will most likely be well.

Link to comment
https://forums.phpfreaks.com/topic/49534-solved-strings-help/#findComment-242841
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.