[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Splitting string into varying length chunks

Posted on 2011-02-16
3
Medium Priority
?
276 Views
Last Modified: 2012-05-11
Hi,

I have a string e.g.

$string = "123ES4GYT6889";

Open in new window


which is always going to be the same length.

I want to split it in the same way each time - the first 3 characters, then the next 6, then the next 3.

What is the most efficient way of doing this?

Thanks,

Julian

0
Comment
Question by:DrZork101
3 Comments
 
LVL 5

Expert Comment

by:LLMorrisson
ID: 34909232
$chunk1 = substr($str, 0, 3);
$chunk2 = substr($str, 3, 6);
$chunk3 = substr($str, 9, 3);
0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 1000 total points
ID: 34910127
An alternative that leaves them in arrays (might be more convenient depending on what you are doing)
<?php

$string = "123ES4GYT6889";
preg_match('#(.{3})(.{6})(.{3})#', $string, $matches );

// bits are in elements 1,2 and 3
//
print_r( $matches );

Open in new window


Produces

Array
(
    [0] => 123ES4GYT688
    [1] => 123
    [2] => ES4GYT
    [3] => 688
)

Lengths can be easily altered by changing the numbers in the braces, e.g. {3} to {4} and so on...
0
 
LVL 11

Accepted Solution

by:
Ovid Burke earned 1000 total points
ID: 34910588
I like bportlock's approach, but assuming a consistent structure to the string i would go further, and validate this as well.

<?php

$string = "123ES4GYT6889";
$a = preg_match("/([0-9]{3})([A-Z]{2}[0-9]{1}[A-Z]{3})([0-9]{3})/", $string, $matches);

print_r($matches);

?>

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month19 days, 13 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question