digitalgod Posted May 1, 2007 Share Posted May 1, 2007 Hey guys, I need to grab a string from a db that looks like this : WLK0001 and simply add +1 to it, it has to stay in the same format... any suggestions? Link to comment https://forums.phpfreaks.com/topic/49534-solved-strings-help/ Share on other sites More sharing options...
Koobi Posted May 1, 2007 Share Posted May 1, 2007 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 More sharing options...
digitalgod Posted May 1, 2007 Author Share Posted May 1, 2007 yup I'm certain that it will always be 4 digits, thanks I'll give it a shot! Link to comment https://forums.phpfreaks.com/topic/49534-solved-strings-help/#findComment-242814 Share on other sites More sharing options...
roopurt18 Posted May 1, 2007 Share Posted May 1, 2007 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. Link to comment https://forums.phpfreaks.com/topic/49534-solved-strings-help/#findComment-242821 Share on other sites More sharing options...
Koobi Posted May 1, 2007 Share Posted May 1, 2007 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 More sharing options...
digitalgod Posted May 1, 2007 Author Share Posted May 1, 2007 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 More sharing options...
roopurt18 Posted May 1, 2007 Share Posted May 1, 2007 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 More sharing options...
Koobi Posted May 1, 2007 Share Posted May 1, 2007 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 https://forums.phpfreaks.com/topic/49534-solved-strings-help/#findComment-242844 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.