Log in

View Full Version : how to split the string in 2 parts



pankaj.ghadge
11-12-2008, 05:42 AM
hello ,

I want split the string into 2 parts.
e.g 1) 10:16:5210999_aa@mydomain.com/jsjac_simpleclient -> 10999_cc/ > hi cc sds ds s / > sdsdsd / > s s sds <

Output:-
str_array[0]=10:16:5210999_aa@mydomain.com/jsjac_simpleclient -> 10999_cc
str_array[1]=hi cc sds ds s / > sdsdsd / >s s sds <

2) 10:17:31 10999_cc/jsjac_simpleclient ->10999_aa/ < d dd ddd<< / >> / << ddd
output:-
str_array[0]=10:17:31 10999_cc/jsjac_simpleclient ->10999_aa
str_array[1]=d dd ddd<< / >> / << ddd


I want to split the string into two part by using separator / > or/ < (There is space between / and >)

i want divide the string only into two part , but other function available in php divides the string into several part if it found separator character.

Consider first appearance of separator to divide the string into two part

So please tell me how to do this . is there any inbuilt function available in php?

...........................................

james438
11-12-2008, 12:36 PM
I have been looking, but not that I can see. I was thinking there might be something listed under preg_split() in php.net, but I couldn't find anything.

Maybe you could explode the $string and then implode it back together without string[0] and then put the two parts into a new array.

pankaj.ghadge
11-13-2008, 08:49 AM
CODE


$s1 = '10:16:52 10999_aa@mydomain.com/jsjac_simpleclient -> 10999_cc@mydomain.com/ > hi cc sds ds s sdsdsd s s sds';
$s2 = '2008-11-11 16:21:59 10999_aa@mydomain.com/jsjac_simpleclient -> 10999_cc@mydomain.com/ < hi1234 / > / > / <';
$htmlbits = preg_split('/(\/ >)|(\/ <)/',$s2,2); // or $str_array = preg_split('/\/ [<>]/', $s1, 2);

print_r($str_array);
echo "<br>";
print_r($htmlbits);

james438
11-13-2008, 06:54 PM
Woot! not that I had the problem, but thanks for that answer. The answer was staring me right in the face and I just didn't see it. I think it was because I was reading more on the syntax as opposed to the preg_replace page at php.net.