0

My problem is not very complex but I can't find a solution yet. I have a string:

$temp_string = '20938999038,0.5,83888999289,0.5,98883888778,0.9';

// Meaning syskey, price, syskey, price, syskey, price

I want to remove price and show only syskey.

My desirable result:

 '20938999038,83888999289,98883888778'

Thanks in advance.

8
  • 5
    why are you hurting yourself and you're not using arrays instead? Commented May 7, 2013 at 13:45
  • Use explode() and work with array. Commented May 7, 2013 at 13:45
  • You need to enclose your string in quotes, otherwise it's not a string. Also have a look at explode and implode, they should point you in the right direction. Commented May 7, 2013 at 13:45
  • $parts=explode(",",$temp_string); Commented May 7, 2013 at 13:46
  • Thanks for quick answers as long as I got desired results.every solution is welcome. Commented May 7, 2013 at 13:48

2 Answers 2

3

You can explode it by delimiter

$explode = explode(",", $temp_string);
unset($explode[1], $explode[3], $explode[5]);

Assuming your string always follows this pattern.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks.What if for longer or shorter inputs?
Well if it follows the same pattern you could continue to add to the unset command. Probably the best solution if it is every other is to do what @Baba recommended and select what values to store based on mod in a loop.
2

You can try:

$string = "20938999038,0.5,83888999289,0.5,98883888778,0.9";

$new = array();
foreach(explode(",", $string) as $k => $v) {
    $k % 2 or $new[] = $v;
}
echo implode(",", $new); // 20938999038,83888999289,98883888778

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.