Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2007
    Location
    Latin America
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How many matches of x word in message?

    Hello, I have tried learning preg_match & preg_match_all, but its no use, can anyone make me a small piece of code that would count how many times x word was found in x message? Please, or at least explain me how to do one.

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    How about this:
    PHP Code:
    $needle="php";
     
    $pattern="/\b".$needle."\b/i";
     
    $haystack="does php rock or does phprock";//deliberate mising space.
     
    print "found:".preg_match_all($pattern$haystack$matches)."\n"//prints found:1 since it doesn't count phprock. 
    Or you could always use substr_count

  • #3
    New Coder
    Join Date
    Jul 2007
    Location
    Latin America
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I had no idea that function was there.... Thanks a lot anyways!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •