All,

I'm beside myself with this one. Any help would be appreciated!

I have a string like:

[var0, var1, var2, var3]

I need to remove everything past the first comma (no matter how many other comma's there are, but including the comma) but only between the brackets as there's more to this string. So the above example would look like this when done:

[var0]

Then I need to remove the brackets around var0 (which I can do!)

The first part is difficult for me, because var0 is never the same length. And I don't know how many comma's (with variables) there will be. I could only have var0 and var1, or var0-var10 for all I know. 😕

Any thoughts on how I could solve this?

    [man]strpos[/man] will find the first comma.
    It also takes an offset, so you can use [man]strpos[/man] to find the "[" (if necessary), then [man]strpos[/man] to find the first comma after that, and then [man]strpos[/man] again to find the first "]" after that.

    Then it's just a matter of combining those results with judicious use of [man]substr[/man].

      Thanks Weedpacket!

      $start = strpos($data, ',');
      $end = strpos($data, ']');

      Everytime I try to work with this, it only keeps whats between those and kills the rest of the string. I switched to (start) [ and , (end) and I certainly have just that part of the string - but the rest of the string is gone.

      How would I tell it to remove just what's between the first " , " and the " ] " and keep the rest of the string?

      I guess what I'm trying to say is, I have no idea how to tell it to stop 🙁

        Like I said, [man]strpos[/man] also takes an offset; you can tell it where to start looking from.
        Since [man]strpos[/man] doesn't remove anything from a string, the problem must be how you're using [man]substr[/man]. Or perhaps [man]substr_replace[/man].

          Thanks again!

          substr_replace was the key I think. And it took me a few minutes to realize the end is offset from the starting point. (ie, 4 in, and I need to go to 10... the end isn't 10, it's 10-4 = 6.) I'm sure that's documented somewhere 🙂

          I'm all set, thanks for taking the time to share your knowledge! 😃

            I only thought of substr_replace while I was writing that post. And yes; the "end" is actually the "length" (it's a toss-up which is the more common; Java uses end, C# uses length, and so on),

              Write a Reply...