Jump to content

splitting a string into a string and an integer.


dazman

Recommended Posts

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;

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

 

 

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.

 

 

 

 

$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;

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.