Splitting a String

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Judson
Forum Newbie
Posts: 3
Joined: Sat Apr 16, 2005 2:23 pm

Splitting a String

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

if you already have the format with the "-" then use explode(). If not, then look at substr(), strlen(), and strpos()
Judson
Forum Newbie
Posts: 3
Joined: Sat Apr 16, 2005 2:23 pm

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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"
Judson
Forum Newbie
Posts: 3
Joined: Sat Apr 16, 2005 2:23 pm

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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);
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply