tags:

views:

55

answers:

3

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.

+6  A: 

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
middus
Hi, what if the testcase looks like this : `o201_01_01_Testing_to_see_If_this_testcases_passes:without_data`with some?
JPro
What do you mean with "with some?"?If there first part is a bit shorter or longer, the above approach won't work and you probably have to use some kind of pattern matching or clever use of [`strpos`](http://php.net/strpos) and the likes. It depends a bit on your format. Could you specify how exactly the first part is formatted? Is it always 10 or 11 characters with underscores (`_`) at position 4 and 7? (i.e. does only the length of the part between the second and third underscore change?)
middus
except with being `o201_01_01a` or `o201_01_01` everything is in their position.
JPro
+1  A: 

$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

jsumners
A: 

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...

Jonah Bron