I've tried to make a tool in which you input a website and when you click the submit button it cURLS all the text.
After all the cURLing, stripping it from tags, and counting the words. It's eventually an array named $frequency
. If I echo it using <pre>
tags it will show me everything just fine! (NOTE: I'm placing the contents in a file, $homepage = file_get_contents($file);
and this is what I work with in my code, I don't know if this matters or not)
However i don't really care if the word or
is seen 200 times in a website, I only want the important words. So i have made an array with all the common words. Which is set eventually in the $common_words
variable. But i can't seem to find a way to replace all words found in the $frequency
to replace them with ""
if they are found in the $common_words
as well.
I've found this piece of code after some research:
$string = 'sand band or nor and where whereabouts foo';
$wordlist = array("or", "and", "where");
foreach ($wordlist as &$word) {
$word = '/\b' . preg_quote($word, '/') . '\b/';
}
$string = preg_replace($wordlist, '', $string);
var_dump($string);
If I copy paste this it works fine, removing the or, and, where
from the string.
But replacing $string
with $frequency
or replacing $wordlist
with $common_words
will either not work or throw me an error like: Delimiter must not be alphanumeric or backslash
I hope i've formulated my question properly, if not. Please tell me!
Thanks in advance
EDIT: Alright, i've narrowed down the problem alot. First of all i forgot the &
inside the foreach ($wordlist as &$word) {
But as it was counting all the words, the words it has replaced are all still counted. See those 2 screenshots to see what I mean: http://imgur.com/oqqZR3h,xHEZKRz#0