Splitting a String
Moderator: General Moderators
Splitting a String
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
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
here:
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"
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";
?>Actually, substr will go until the end of the string if you don't give it a third argument. So:
$third = substr($bob,9,$len);
you don't give it a third argument. So:
ill go until the end of the string if you don't give it a third argument. So:
ill go until the end of the string if you don't give it a third argument. So:
.]
$len = strlen($bob);
$third = substr($bob,9,$len);
third = substr($bob,9,$len);
nd of the string if you don't give it a third argument. So:
on't give it a third argument. So:
,9,$len);
rgument. So:
a third argument. So:
substr($bob,9);
ill go until the end of the string if you don't give it a third argument. So:
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 = substr($bob,9);Code: Select all
$len = strlen($bob);
$third = substr($bob,9,$len);Code: Select all
$third = substr($bob,9);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);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);$len = strlen($bob);
$third = substr($bob,9,$len);
Code: Select all
$third = substr($bob,9);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);Code: Select all
$len = strlen($bob);
$third = substr($bob,9,$len);Code: Select all
$third = substr($bob,9);Code: Select all
$third = substr($bob,9);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);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);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.