开发者

splitting strings in php

开发者 https://www.devze.com 2022-12-23 01:07 出处:网络
I have some testcases/strings in this format: o201_01_01a_Testing_to_see_If_this_testcases_passes:without_data

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开发者_如何学C_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.


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



$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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消