Php search string

Discussion in 'PHP' started by jcnkty, Sep 14, 2007.

  1. #1
    guys can you help me with my problem regarding search for a string.


    $var = "81,82,83,84,1460,1461,1462,1463,1464,1465,1466,1467,1468,1469,1470,1471,1472,1473,1474,1475,1476,1477,1478,1479,1480,1481,1482,1483,1484,1485,1486";

    $var2 = "84";

    i want to check if the var2 is in the $var

    I use ereg and i keep on getting the 84 and the 1484

    i want a script to only gets the 84 value.
     
    jcnkty, Sep 14, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    
    if (in_array($var2, explode(',', $var)))
    {
        // Found
    }
    
    PHP:
     
    nico_swd, Sep 14, 2007 IP
  3. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $var = explode(',', $var)
    if (in_array($var2, $var)) {
    echo "Its there.";
    }
     
    tamen, Sep 14, 2007 IP
  4. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Dammit! Beat by one minute ;)
     
    tamen, Sep 14, 2007 IP
  5. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #5
    Not that it is significant enough for something trivial like this, a preg_match() is a fair bit faster than using an array for this.

    if (preg_match("/(^|,)$var2(,|\$)/", $var)) {
      // Match found
    }
    PHP:
     
    krt, Sep 14, 2007 IP
  6. jcnkty

    jcnkty Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thanks for a quick reply guys!
     
    jcnkty, Sep 14, 2007 IP
  7. DopeDomains

    DopeDomains Guest

    Messages:
    207
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Quicker than explode. Quicker for me to code than regular expressions. But not the fastest, or the most elegant:
    
    if (!(strpos($var,$var2)===false)) {
        // string found.
    }
    
    PHP:
    -Jason
     
    DopeDomains, Sep 15, 2007 IP
  8. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I like that one.
     
    tamen, Sep 15, 2007 IP
  9. aRo`

    aRo` Peon

    Messages:
    141
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    just to expand te list :)

    if(eregi($var1,$var2)){
    echo "ok"
    }
     
    aRo`, Sep 15, 2007 IP
  10. ashrafweb

    ashrafweb Member

    Messages:
    67
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #10
    goood topic
     
    ashrafweb, Sep 15, 2007 IP
  11. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Anybody else have another way to do it?
     
    tamen, Sep 15, 2007 IP
  12. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #12
    DopeDomains and aRo, read the question again!

    While both of your code match "84", they also incorrectly match "1484", hence the need for regex as in my solution or exploding the list by commas as nico suggested.
     
    krt, Sep 15, 2007 IP
  13. DopeDomains

    DopeDomains Guest

    Messages:
    207
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #13
    
    if (!(strpos(",$var",",$var2,")===false)) {
        // string found.
    }
    Better?
    
    PHP:
     
    DopeDomains, Sep 16, 2007 IP
  14. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #14
    That will work, DopeDomains, although you don't need the trailing comma on $var2.
     
    tamen, Sep 16, 2007 IP
  15. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #15
    I suppose :p

    But you are missing a comma, probably a typo. And tamen, the trailing comma is needed.

    So it would be:
    if (!(strpos(",$var,",",$var2,")===false)) {
        // string found.
    }
    PHP:
     
    krt, Sep 16, 2007 IP
  16. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Its needed if you put a comma on both sides of $var2, not if you only put a comma in front of $var2.

    But, we are splitting hairs :) I doubt you could even measure the difference in execution time.
     
    tamen, Sep 16, 2007 IP
  17. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #17
    No wait. Thats gibberish. If you put a comma in front of $var, every number will have a comma in front of them. Then you only need a comma in front of $var2
     
    tamen, Sep 16, 2007 IP
  18. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #18
    Both vars need to be enclosed in commas, in case the number we're looking for is the first or the last.
     
    nico_swd, Sep 16, 2007 IP
  19. DopeDomains

    DopeDomains Guest

    Messages:
    207
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #19
    Two commas good.. one comma bad. I did in fact miss the second comma on $var.

    -Jason
     
    DopeDomains, Sep 16, 2007 IP