anujgarg Posted February 24, 2010 Share Posted February 24, 2010 Hi Everyone, I am facing a problem while splitting a concatenated string. The problem is: I have a concatenated string that is being appended in a foreach loop. All I need to do is to fetch the value from the last string in every concatenation. For eg. I have a string -> "a:10 b:20 c:30". This is concatenated by another string and looks like -> "a:10 b:20 c:30 a:50 b:60 c:70". This is again concatenated by another string and looks as -> "a:10 b:20 c:30 a:50 b:60 c:70 a:150 b:160 c:170" and so on. There is no delimeter to separate this string at the moment. I am trying to fetch the value of "c" in the last string of every concatenation/combination ie I want -> c:30 c:70 c:170 I have tried to convert it to array first and then iterate it but didn't find the desired solution. Please suggest how can I solve this problem. Any suggestion will be highly appreciated. Thanks Anuj Link to comment https://forums.phpfreaks.com/topic/193253-split-concatenated-string/ Share on other sites More sharing options...
Goat Posted February 24, 2010 Share Posted February 24, 2010 $input = "a:10 b:20 c:30 a:50 b:60 c:70 a:150 b:160 c:170"; $arr_input = explode(' ', $input); foreach($arr_input as $value) { $arr_value = explode(':',$value); if($arr_value[0] == 'c') { $output .= $arr_value[0].':'.$arr_value[1].' '; } } echo $output; that's it Goat Link to comment https://forums.phpfreaks.com/topic/193253-split-concatenated-string/#findComment-1017568 Share on other sites More sharing options...
sasa Posted February 24, 2010 Share Posted February 24, 2010 try <?php $input = "a:10 b:20 c:30 a:50 b:60 c:70 a:150 b:160 c:170"; preg_match_all('/c:(\d+)/',$input,$out); print_r($out); ?> Link to comment https://forums.phpfreaks.com/topic/193253-split-concatenated-string/#findComment-1017585 Share on other sites More sharing options...
anujgarg Posted February 24, 2010 Author Share Posted February 24, 2010 Thanks for the quick replies... Link to comment https://forums.phpfreaks.com/topic/193253-split-concatenated-string/#findComment-1017592 Share on other sites More sharing options...
anujgarg Posted February 25, 2010 Author Share Posted February 25, 2010 I have some problem further: After applying the following: preg_match_all('/c:(\d+)/',$input,$out); print_r($out); I am getting this: Array ( [0] => Array ( [0] => Duration: 00:01:25.67, start [1] => Duration: 00:01:29.48, start ) [1] => Array ( [0] => 00:01:25.67 [1] => 00:01:29.48 ) ) Array ( [0] => Array ( [0] => Duration: 00:01:25.67, start [1] => Duration: 00:01:29.48, start [2] => Duration: 00:01:29.48, start ) [1] => Array ( [0] => 00:01:25.67 [1] => 00:01:29.48 [2] => 00:01:29.48 ) ) Array ( [0] => Array ( [0] => Duration: 00:01:25.67, start [1] => Duration: 00:01:29.48, start [2] => Duration: 00:01:29.48, start [3] => Duration: 00:00:57.43, start ) [1] => Array ( [0] => 00:01:25.67 [1] => 00:01:29.48 [2] => 00:01:29.48 [3] => 00:00:57.43 ) ) and so on I am looking for the result from the last Array statement: Array ( [0] => 00:01:25.67 [1] => 00:01:29.48 [2] => 00:01:29.48 [3] => 00:00:57.43 I want to traverse this array and keep storing each value in variables. For eg. a = 00:01:25.67, b = 00:01:29.48 and so on. Is this the right way: for($i=0;$i<count($var);$i++) { $var[$i]; } How should I do it? TIA Anuj Link to comment https://forums.phpfreaks.com/topic/193253-split-concatenated-string/#findComment-1017844 Share on other sites More sharing options...
alpine Posted February 25, 2010 Share Posted February 25, 2010 Work with something like this <?php $input = "a:10 b:20 c:30 a:50 b:60 c:70 a:150 b:160 c:170"; preg_match_all('/c:(\d+)/',$input,$out); $out = array_reverse($out); while(list($key, $val) = each($out[0])){ ${$key} = $val; } ?> Link to comment https://forums.phpfreaks.com/topic/193253-split-concatenated-string/#findComment-1017885 Share on other sites More sharing options...
anujgarg Posted February 25, 2010 Author Share Posted February 25, 2010 I have done it as: for($i=0;$i<count($var[1]);$i++) { $input = rtrim($var[1][$i] . " "); } Link to comment https://forums.phpfreaks.com/topic/193253-split-concatenated-string/#findComment-1017887 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.