How do i count number of words

Discussion in 'PHP' started by Bohra, Nov 21, 2009.

  1. #1
    Well i want to output how many times a word is repeated on an external site how would i do ths ??

    can some1 post a sample code using file_get_contents
     
    Bohra, Nov 21, 2009 IP
  2. ShadyStudent

    ShadyStudent Peon

    Messages:
    341
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Would something like this work (not tested):

    $count = 0;
    while($contents = file_get_contents("URL")){
    
          $count = substr_count($contents,"$your_string");
    
    }
    PHP:
    Might have to use if rather than while and use that code in some sort of loop.
     
    ShadyStudent, Nov 21, 2009 IP
  3. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #3
    Is it a good idea to loop coz it will connect the page everytime
     
    Bohra, Nov 21, 2009 IP
  4. getquicksolution

    getquicksolution Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    May this code solve your problem:
    $count = 0;
    $t=0;
    $contents = file_get_contents("URL");
    $length=strlen($contents);
    for ($i=0;$i<$length;$i++);
    if($contents[$i]==" " && $contents[$i+1]!=" ")
    {$arrstr[$t]=$contents[$i+1];
    $t++;
    }
    $len=count($arrstr);
    $ct=0;
    for ($i=0;$i<$len;$i++);
    {
    for ($j=0;$j<$length;$j++)
    {if($arrstr[$i]==$contents[$j])
    $ct++;
    }

    $ntime[$q]=$ct;
    $ct=0;

    }


    $j=0;
    for ($i=0;$i<$len;$i++);
    {
    echo "Number of time the words" . $arrstr[$i]."repeats is".$ntime[$j];
    $j++;
    }


    I have not tested it.
     
    getquicksolution, Nov 21, 2009 IP
  5. ShadyStudent

    ShadyStudent Peon

    Messages:
    341
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You can easily modify that and set a variable to hold that info and then check if end of file has been reached in the while loop.
     
    ShadyStudent, Nov 26, 2009 IP
  6. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #6
    $url = 'http://www.google.com/';
    $contents = strtolower(strip_tags(file_get_contents($url)));
    $contents = preg_replace('%[^\w\s\r\n]%', ' ', $contents);
    $words = preg_split('%[\s\r\n]+%', $contents);
    $wordcounts = array_count_values($words);
    asort($wordcounts);
    echo '<pre>'.print_r($wordcounts, true).'</pre>';
    PHP:
    Something like that? Just use
    echo $wordcounts['word_goes_here'];
     
    JAY6390, Nov 26, 2009 IP
  7. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #7
    How could i do the same thing in curl basically it has to login and check
     
    Bohra, Nov 26, 2009 IP
  8. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
  9. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #9
    so basically after fetching the url we just have to use the rest of the code u posted

    $contents = strtolower(strip_tags(file_get_contents($url)));
    $contents = preg_replace('%[^\w\s\r\n]%', ' ', $contents);
    $words = preg_split('%[\s\r\n]+%', $contents);
    $wordcounts = array_count_values($words);
    asort($wordcounts);
    echo '<pre>'.print_r($wordcounts, true).'</pre>';

    i am not sure abt the curl thing so if u can confirm
     
    Bohra, Nov 27, 2009 IP
  10. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #10
    yep. make sure you change $content to $contents or vice versa
     
    JAY6390, Nov 27, 2009 IP
  11. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #11
    $contents = strtolower(strip_tags(file_get_contents($url)));

    would be

    $contents = strtolower(strip_tags($url)));

    rite ??
     
    Bohra, Nov 27, 2009 IP
  12. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #12
    no change $url to $content
     
    JAY6390, Nov 27, 2009 IP