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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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