Saturday, April 27, 2013

Update to "Please Don't Grab!"

After someone challenged me to write some type of program/script to find the frequency of the word grab occurring in a webpage I have an update for you.

The following two lines get the job done: 
$str = file_get_contents('URL'); //gets the contents of the webpage

print_r(array_count_values(str_word_count(strip_tags(strtolower($str)), 1))); //prints a list of all words found

The second line:
  •  converts all words to lower case using strtolower
  •  then it uses strip_tags to remove the html tags
  • str_word_count returns information about words used in a string
  • and array_count_values counts the values of the array
I copied and pasted a few URLs from the Pioneer Woman's website and ran the script to produce the following:

Link: http://thepioneerwoman.com/cooking/2010/08/fried-round-steak/ found 1 occurrence
Link: http://thepioneerwoman.com/cooking/2010/08/homemade-cinnamon-bread/ found 1 occurrence
Link: http://thepioneerwoman.com/cooking/2010/07/16-minute-meal-3-mini-meatball-sandwiches/ found 4 occurrences
Link: http://thepioneerwoman.com/cooking/2010/04/the-right-way-and-the-wrong-way-to-make-cinnamon-toast/ found 2 occurrences
Link: http://thepioneerwoman.com/cooking/2010/04/sloppy-joes/ found 0 occurrences
Link http://thepioneerwoman.com/cooking/2010/02/homemade-glazed-doughnuts/ found 2 occurrences
Link http://thepioneerwoman.com/cooking/2010/01/homemade-chicken-and-noodles/ found 2 occurrences
Link: http://thepioneerwoman.com/cooking/2010/01/perfect-potatoes-au-gratin/ found 1 occurrence
Link: http://thepioneerwoman.com/cooking/2009/10/simple-pan-fried-pork-chops found 1 occurrence
Link: http://thepioneerwoman.com/cooking/2009/10/simple-pan-fried-pork-chops/ found 1 occurrence
Link: http://thepioneerwoman.com/cooking/2009/07/vanilla-bean-ice-cream/ found 1 occurrence
Link: http://thepioneerwoman.com/cooking/2009/07/edna-maes-escalloped-cabbage-recipe/ found 0 occurrencesLink: http://thepioneerwoman.com/cooking/2009/05/perfect-pancakes/ found 1 occurrence
Link: http://thepioneerwoman.com/cooking/2009/04/creamed-spinach-to-die-for/ found 1 occurrence
Link: http://thepioneerwoman.com/cooking/2009/04/macaroni-cheese/ found 0 occurrences
Link: http://thepioneerwoman.com/cooking/2009/03/bbq-meatballs-comfort-food-to-the-max/ found 1 occurrence
Link: http://thepioneerwoman.com/cooking/2009/02/spreads-my-brothers-favorite-cookie/ found 3 occurrences

 These 17 URLs found a total of 22 occurrences of the word grab.

No comments:

Post a Comment