JPro
JPro

Reputation: 6556

Split string on 3rd instance of delimiter

I have some testcases/strings in this format:

o201_01_01a_Testing_to_see_If_this_testcases_passes:without_data
o201_01_01b_Testing_to_see_If_this_testcases_passes:data
rx01_01_03d_Testing_the_reconfiguration/Retest:

Actually this testcase name consists of the actual name and the description.

So, I want to split them like this :

o201_01_01a   Testing_to_see_If_this_testcases_passes:without_data
o201_01_01b   Testing_to_see_If_this_testcases_passes:data
rx01_01_03d   Testing_the_reconfiguration/Retest:

I am unable to figure out the exact way to do this in explode in php

Can anyone help please?

Thanks.

Upvotes: 0

Views: 124

Answers (3)

Jonah
Jonah

Reputation: 10091

Looking at the pattern, it appears that you need to use regular expressions. If this is how they all are, you can cut off the beginning by looking for an upper case character. The code might look like this:

$matches = array()
preg_match('/^[^A-Z]*?/', $string, $matches);
$matches = substr($matches[0], 0, count($matches[0])-1);

Would put the first little part into $matches. Working on second part...

Upvotes: 0

James Sumners
James Sumners

Reputation: 14783


$results = preg_split("/([a-z0-9]+_[0-9]+_[0-9]+[a-z])(.*)/", $input);

That should give you an array of results, provided I got the regular expression correct.

http://www.php.net/manual/en/function.preg-split.php

Upvotes: 1

middus
middus

Reputation: 9121

If the first part has always the same length, why don't you use substr, e.g.

$string = "o201_01_01a_Testing_to_see_If_this_testcases_passes:without_data";
$first_part = substr($string, 0, 11); // o201_01_01a
$second_part = substr($string, 12); // Testing_to_see_If_this_testcases_passes:without_data

Upvotes: 6

Related Questions