Jump to content

Split concatenated string


anujgarg

Recommended Posts

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

$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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.