How many instances of a string?

Discussion in 'PHP' started by mjmorrell, Jan 8, 2008.

  1. #1
    Can someone please post a code that will return a number of how many instances of a string are in a string? For example:

    "my name is tim tim is a dumb name i hate people named tim"

    I want a code to return "3" (for the 3 instances of tim) for that. Please help!
     
    mjmorrell, Jan 8, 2008 IP
  2. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #2
    commandos, Jan 8, 2008 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    nico_swd, Jan 9, 2008 IP
  4. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #4
    Thanks nico :)

    but would this return the same if the word was a part of a word like "tim went to timhorton" substr_count would return 2 while the real would return 1 . right ?
     
    commandos, Jan 9, 2008 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    Yep, that's right.

    However, the OP didn't state whether he wants to count these occurrences or not. We'll see I guess. :p
     
    nico_swd, Jan 9, 2008 IP
  6. daringtakers

    daringtakers Well-Known Member

    Messages:
    808
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    110
    #6
    Hey nico_swd you are zend certified .. gr8
    M sun certified.
     
    daringtakers, Jan 9, 2008 IP
  7. midhunhk

    midhunhk Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    in javascript, you can easily do that using regular expressions. i guess thats available in php as well. pls chek on that ..........
     
    midhunhk, Jan 9, 2008 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Yes I am. And nice one. I never got around Java.

    Yes, you could also use preg_match_all(). It might even be faster than the explode/foreach version. Not sure though.

    
    
    $word = 'tim';
    $occurrences = preg_match_all("~(^|\s){$word}(\s|$)~", $string);
    
    echo $occurrences;
    
    PHP:
    Untested but should do the job as well.
     
    nico_swd, Jan 9, 2008 IP
  9. Sabbir

    Sabbir Banned

    Messages:
    210
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #9
    Why don't you go to w3schools.com.
    I hope then you can find it by your self.


    best wishes.
    _______________________
    Custom lanyards are here.
     
    Sabbir, Jan 10, 2008 IP
  10. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #10
    you should be banned

    1- your post is worthless
    2- you are spamming with a fake link in your signature
    3- we are not stupid here ;) it's DP grow up kid
     
    commandos, Jan 10, 2008 IP