dazman Posted June 16, 2009 Share Posted June 16, 2009 Hi I am trying to split a string into 2 pieces. I have these address ID's and they come out of the database in this format ADID00000001. I want to split the ADID and make that a string, and the 00000001 as an integer. I read the php manual on split, and on explode, I assume I use the explode function to do this. I also want to put it back together later on. Would I use implode? Here is my first attempt at splitting it, but it obviously doesnt work. Any help? $checkaddress = mysql_query("SELECT Address_ID FROM address ORDER BY Address_ID DESC"); $lastADID = mysql_result($checkaddress,0,"Address_ID"); mysql_close(); $lastADID = array(explode ( string 'ADID', string $prefix [, int $endno ] )); echo $prefix; echo $endno; Quote Link to comment https://forums.phpfreaks.com/topic/162360-splitting-a-string-into-a-string-and-an-integer/ Share on other sites More sharing options...
RichardRotterdam Posted June 16, 2009 Share Posted June 16, 2009 Before you start coding It might be wise to answer yourself a couple of questions first. if you have the following string ADID00000001 You can check the following conditions: 1. Is it always the first 4 characters that are letters? 2. Is ADID a prefix that is always the same or can it have different letters. 3. Do the letters have a range of the alphabet or is it hexdecimal? 4. Is the second part of this string always an 8 digit string. Maybe there are a couple more of these but with that input you can code a whole lot easier. You also might want to look into regular expressions. It might be the way to go with this case Quote Link to comment https://forums.phpfreaks.com/topic/162360-splitting-a-string-into-a-string-and-an-integer/#findComment-856947 Share on other sites More sharing options...
dazman Posted June 16, 2009 Author Share Posted June 16, 2009 I am looking at this function now.. substr(). 1) Yes, the first 4 characters will always be letters. 2) It may have different letters in the future. 3) No its not hex, this is just data that is stored in the database. Its just text. 4) The last 8 characters is always intended to be part of the string. The field in the database is VARCHAR(12) so it wont ever be longer that 12 characters. Quote Link to comment https://forums.phpfreaks.com/topic/162360-splitting-a-string-into-a-string-and-an-integer/#findComment-856948 Share on other sites More sharing options...
bibliovermis Posted June 16, 2009 Share Posted June 16, 2009 $checkaddress = mysql_query("SELECT Address_ID FROM address ORDER BY Address_ID DESC"); $lastADID = mysql_result($checkaddress,0,"Address_ID"); mysql_close(); $lastADID = array(explode ( string 'ADID', string $prefix [, int $endno ] )); echo $prefix; echo $endno; 1) Yes, the first 4 characters will always be letters. 2) It may have different letters in the future. 3) No its not hex, this is just data that is stored in the database. Its just text. 4) The last 8 characters is always intended to be part of the string. The field in the database is VARCHAR(12) so it wont ever be longer that 12 characters. Given those conditions, substr is indeed the appropriate function. $letters=substr($lastADID,0,4); $numbers=substr($lastADID,4); To recombine later, simply put a '.' between them. Implode will work, but it is overkill for combining two pieces. $recombined=$letters.$numbers; Quote Link to comment https://forums.phpfreaks.com/topic/162360-splitting-a-string-into-a-string-and-an-integer/#findComment-857185 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.