Results 1 to 14 of 14

Thread: word occurence

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    Osaka
    Posts
    200

    word occurence

    I know fopen function is to open website (handle) but don't know how to search for number of occurences of particular word.
    Would you help me ?
    Thank You.

  2. #2
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: word occurence

    Look at preg_match and preg_match_all, it allows you to match all instances of words and patterns

    http://uk.php.net/manual/en/function.preg-match.php

  3. #3
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: word occurence

    A few problems with preg... it will match test in testing for example easy too fix but try something like this:

    PHP Code:
    <?php
      
    function NumInstancesOfWord($HayStack$Needle$MatchCase false)
      {
        
    $WordArray = array();
        
    # Make shure nasty chars don't affect the results.
        
    $HayStack  preg_replace('#[^a-z0-9 ]#si'''$HayStack);
        if (
    $MatchCase == false# Case insensative
        
    {
          
    $WordArray explode(' 'strtolower($HayStack));
          
    $Needle      strtolower($Needle);
        }
        else
        {
          
    $WordArray explode(' '$HayStack);
        }
            
        
    $CountVar 0;
        
    $i        0;
        while (
    $WordArray[$i])
        {
          if (
    $WordArray[$i] == $Needle)
          {
            ++
    $CountVar;
          }
          ++
    $i;
        }
        return (
    $CountVar);
      }
    ?>
    Example:

    PHP Code:
    <?php
      
    echo NumInstancesOfWord('This is a test, testing 123, testing 1123''test'); # Returns 1
      
    echo NumInstancesOfWord('This is a test, testing 123, testing 1123''testing '); # Returns 2
    ?>
    Cheers,

    Ryan Jones
    Last edited by sciguyryan; Feb 1st, 2006 at 03:45 PM.
    My Blog.

    Ryan Jones.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    Osaka
    Posts
    200

    Re: word occurence

    Undefined offset: 8

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    Osaka
    Posts
    200

    Re: word occurence

    and both are returning 1.

  6. #6
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: word occurence

    Quote Originally Posted by slice
    and both are returning 1.
    http://standards.spiralmindsinc.com/Testing/Test.php

    There are 2 instances of testing.
    There are 1 instances of test.
    This is a test, testing 123, testing 1123
    Exact code in file attatched.

    Cheers,

    Ryan Jones
    Attached Files Attached Files
    My Blog.

    Ryan Jones.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    Osaka
    Posts
    200

    Re: word occurence

    You must spread some Reputation around before giving it to sciguyryan again.
    THANKS A LOT

  8. #8
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: word occurence

    Quote Originally Posted by slice
    You must spread some Reputation around before giving it to sciguyryan again.
    THANKS A LOT
    No problem

    Cheers,

    Ryan Jones
    My Blog.

    Ryan Jones.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    Osaka
    Posts
    200

    Re: word occurence

    EEEEEEK
    Another problem/question now i want to use this function for counting words on particular website page like www.anyname.com/home.asp ...
    I am dump again
    It is always returning me 0 result.

  10. #10
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: word occurence

    Quote Originally Posted by slice
    EEEEEEK
    Another problem/question now i want to use this function for counting words on particular website page like www.anyname.com/home.asp ...
    I am dump again
    It is always returning me 0 result.
    I'm not shure what you mean, the name of the site or the contents of the file?

    If the second the contents can be grabbed as follows:


    PHP Code:
    <?php
      $Contents 
    implode(''file('www.anyname.com/home.asp'));
    ?>
    Then just plug that in as a parameter

    Cheers,

    Ryan Jones
    My Blog.

    Ryan Jones.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    Osaka
    Posts
    200

    Re: word occurence

    Not working
    Code:
    <?php
    	function NumInstancesOfWord($HayStack, $Needle, $MatchCase = false)
    	{
    	  $WordArray = array();
    		# Make shure masty chars don't affect the results.
    		$HayStack  = preg_replace('#[^a-z0-9 ]#si', '', $HayStack);
    	  if ($MatchCase == false) # Case insensative
    		{
    	    $WordArray = explode(' ', strtolower($HayStack));
    			$$Needle   = strtolower($Needle);
    	  }
    		else
    		{
    	    $WordArray = explode(' ', $HayStack);
    		}
    		
    		$CountVar = 0;
    		$i        = 0;
    		while ($WordArray[$i])
    		{
    		  if ($WordArray[$i] == $Needle)
    			{
    			  ++$CountVar;
    			}
    		  ++$i;
    		}
    		return ($CountVar);
    	}
    	$TestStr = 'This is a test, test ing 123, testing 1123';
    	echo ('There are ' . NumInstancesOfWord($TestStr, 'testing') . ' instances of testing.<br />');
    	echo ('There are ' . NumInstancesOfWord($TestStr, 'test')    . ' instances of test.<br />');
    	$Contents = implode('', file('http://vbforums.com/showthread.php?p=2338266#post2338266'));
    	//echo($Contents);
    	echo ('There are ' . NumInstancesOfWord($Contents, 'slice')    . ' instances of slice.<br />');
    //	echo ($TestStr);
    ?>
    Result
    There are 0 instances of slice

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    Osaka
    Posts
    200

    Re: word occurence

    Not working
    PHP Code:
    <?php
        
    function NumInstancesOfWord($HayStack$Needle$MatchCase false)
        {
          
    $WordArray = array();
            
    # Make shure masty chars don't affect the results.
            
    $HayStack  preg_replace('#[^a-z0-9 ]#si'''$HayStack);
          if (
    $MatchCase == false# Case insensative
            
    {
            
    $WordArray explode(' 'strtolower($HayStack));
                $
    $Needle   strtolower($Needle);
          }
            else
            {
            
    $WordArray explode(' '$HayStack);
            }
            
            
    $CountVar 0;
            
    $i        0;
            while (
    $WordArray[$i])
            {
              if (
    $WordArray[$i] == $Needle)
                {
                  ++
    $CountVar;
                }
              ++
    $i;
            }
            return (
    $CountVar);
        }
        
    $TestStr 'This is a test, test ing 123, testing 1123';
        echo (
    'There are ' NumInstancesOfWord($TestStr'testing') . ' instances of testing.<br />');
        echo (
    'There are ' NumInstancesOfWord($TestStr'test')    . ' instances of test.<br />');
        
    $Contents implode(''file('http://vbforums.com/showthread.php?p=2338266#post2338266'));
        
    //echo($Contents);
        
    echo ('There are ' NumInstancesOfWord($Contents'slice')    . ' instances of slice.<br />');
    //    echo ($TestStr);
    ?>
    Result
    There are 0 instances of slice

  13. #13
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: word occurence

    This version seems too work better.

    The problem was that I checked for only the character, not the instance of the needle.

    Cheers,

    Ryan Jones
    Attached Files Attached Files
    My Blog.

    Ryan Jones.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    Osaka
    Posts
    200

    Re: word occurence

    You are my hero
    Thanks a lot.I will learn from you in future too.
    Thank You once again.

Posting Permissions

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



Click Here to Expand Forum to Full Width