Page 1 of 1

Splitting a String

Posted: Tue Jul 05, 2005 2:52 pm
by Judson
Something I have been trying to get for a while is this:
the number " 80001-99-005 " translates to 800010990005 when encoded by our barcoding script (because "-" is not allowed by I25 barcoding format). So I need to write a php function that will split the number into 3 segments. The first segment needs to be the first 5 characters, 80001. The second segment needs to be the next 3 characters 099. And if there is three more characters, I need that to be the last segment.

I guess what I am asking is whether or not there is a PHP function that can do what I need. Any help will be appreciated.

[edit] I would really like for each split segment to be contained in its own variable, like $seg1, $seg2, $seg3. Or an array would work fine [/edit]

Jud

Posted: Tue Jul 05, 2005 3:03 pm
by Burrito
if you already have the format with the "-" then use explode(). If not, then look at substr(), strlen(), and strpos()

Posted: Tue Jul 05, 2005 3:32 pm
by Judson
I know that I could explode them at "-". but the "-" are replaced with 0's because the I25 barcode format doesn't support "-". I will check out the other suggestions u gave :-)
Thanks man.

Jud

Posted: Tue Jul 05, 2005 3:41 pm
by Burrito
here:

Code: Select all

<?
$bob = "800010990005";
$len = strlen($bob);
$first = substr($bob,0,5);
$second = substr($bob,6,2);
$third = substr($bob,9,$len);
echo "$first-$second-$third";
?>
you don't really need to get the length of the string, but it will definitely cover your third section by using it, then you don't need to put some arbitrary number in there as a "guess"

Posted: Wed Jul 06, 2005 9:46 am
by Judson
Haha, your code looks a lot like mine :-) ... thanks for telling me about strlen. I had been looking for a function like that.

Jud

Posted: Wed Jul 06, 2005 10:44 am
by pickle
Actually, substr will go until the end of the string if you don't give it a third argument. So:

Code: Select all

$len = strlen($bob);
$third = substr($bob,9,$len);

Code: Select all

$third = substr($bob,9);

Code: Select all

$len = strlen($bob);
$third = substr($bob,9,$len);

Code: Select all

$third = substr($bob,9);
$third = substr($bob,9,$len);

Code: Select all

$third = substr($bob,9);
you don't give it a third argument. So:

Code: Select all

$len = strlen($bob);
$third = substr($bob,9,$len);

Code: Select all

$third = substr($bob,9);
ill go until the end of the string if you don't give it a third argument. So:

Code: Select all

$len = strlen($bob);
$third = substr($bob,9,$len);

Code: Select all

$third = substr($bob,9);
ill go until the end of the string if you don't give it a third argument. So:

Code: Select all

$len = strlen($bob);
$third = substr($bob,9,$len);

Code: Select all

$third = substr($bob,9);
.]
$len = strlen($bob);
$third = substr($bob,9,$len);

Code: Select all

$third = substr($bob,9);
third = substr($bob,9,$len);

Code: Select all

$third = substr($bob,9);
nd of the string if you don't give it a third argument. So:

Code: Select all

$len = strlen($bob);
$third = substr($bob,9,$len);

Code: Select all

$third = substr($bob,9);
on't give it a third argument. So:

Code: Select all

$len = strlen($bob);
$third = substr($bob,9,$len);

Code: Select all

$third = substr($bob,9);
,9,$len);

Code: Select all

$third = substr($bob,9);
rgument. So:

Code: Select all

$len = strlen($bob);
$third = substr($bobill go until the end of the string if you don't give it a third argument.  So:

Code: Select all

$len = strlen($bob);
$third = substr($bob,9,$len);

Code: Select all

$third = substr($bob,9);
a third argument. So:

Code: Select all

$len = strlen($bob);
$third = substr($bob,9,$len);

Code: Select all

$third = substr($bob,9);
substr($bob,9);
ill go until the end of the string if you don't give it a third argument. So:

Code: Select all

$len = strlen($bob);
$third = substr($bob,9,$len);

Code: Select all

$third = substr($bob,9);
tring if you don't give it a third argument. So:

Code: Select all

$len = strlen($bob);
$third = substr($bob,9,$len);

Code: Select all

$third =
$len = strlen($bob);
$third = substr($bob,9,$len);

Code: Select all

$third = substr($bob,9);