String manipulation help.

Discussion in 'PHP' started by Ntech, Sep 5, 2009.

  1. #1
    Hello,

    Given the following strings:

    4,15,21,27,30,57,75,78,83
    4,20,21,31,75,78
    4,14,15,21,27,30,57,75

    How would I find the first number in each of them which isn't a 4 or 15?

    I'll always know which numbers I don't want, but I won't know what numbers will be in each string.

    It's also possible some of the strings will have neither a 4 or 15 in them.

    Thank you!
     
    Ntech, Sep 5, 2009 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,903
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    I'd explode them into arrays and then foreach through the arrays until I have a "winner", then break to get out.

    even pass the string to a function to do the work to make it tidier.
     
    sarahk, Sep 5, 2009 IP
  3. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #3
    preg_replace where those numbers are at the start of the string "^(4|15)," - in the middle of the string, ",(4|15)," - at the end of the string, ",(4,15)$" - then just explode on , and take the first value.
     
    premiumscripts, Sep 5, 2009 IP
  4. Ntech

    Ntech Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Exploding it into an array worked for me.

    Thanks a lot!
     
    Ntech, Sep 5, 2009 IP