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
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
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
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
Share on other sites

usually, an SQL string manipulation would work faster than a PHP string manipulation so you might want to give roopurts method out. but since it's a CONCAT function, you might want to take a benchmark and compare the two results.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.