Common words found

kanaga ramu
kanaga ramu used Ask the Experts™
on
hai.,  below is my request
 i have a textfile and paragraph.Now i need to grab the words  which are  common to both.

here string($searchthis) is  tested instead of paragraph.


please help me..
Thanks in advance!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016

Commented:
What code have you tried?  This is a fairly common problem in programming class, right?
Most Valuable Expert 2011
Top Expert 2016
Commented:
If you're new to PHP and want to learn the language, this article can help.  Just skip over the parts you already know from work experience or academic study.
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
<?php // demo/temp_ramu.php
/**
 * https://www.experts-exchange.com/questions/29012186/Common-words-found.html
 */
error_reporting(E_ALL);
echo '<pre>';

// A TEXT TO BUILD A CONCORDANCE
$text = <<<EOD
This is the list of words that should be excluded from the lookup.  Mostly these
are common words without "term of art" meanings.  You might want to add to this
list to suppress false-positive matches in your industry.  If you're a church
you might want to exclude "bible" or "worship"; if you're a car dealership you
might want to exclude "car" from the search, etc.
EOD;

// A COLLECTION OF WORDS TO SEARCH FOR IN THE TEXT
$find = <<<EOD
might
exclude
gooseball
EOD;

// BUILD THE CONCORDANCE
$text = preg_replace('/[^A-Z]/i', ' ', $text); // ALPHABET ONLY
$text = preg_replace('/\s\s+/',   ' ', $text); // NORMALIZE WHITESPACE
$text = explode(' ', $text);

$list = [];
foreach ($text as $word)
{
    $word = strtolower($word);
    if (!array_key_exists($word, $list)) $list[$word] = 0;
    $list[$word]++;
}

// FIND THE OCCURRENCES OF THE SEARCH WORDS
$find = preg_replace('/[^A-Z]/i', ' ', $find); // ALPHABET ONLY
$find = preg_replace('/\s\s+/',   ' ', $find); // NORMALIZE WHITESPACE
$find = explode(' ', $find);

foreach ($find as $word)
{
    if (array_key_exists($word, $list))
    {
        echo PHP_EOL . "$word: " . $list[$word];
    }
    else
    {
        echo PHP_EOL . "$word not found";
    }
}

Open in new window

Outputs:
might: 3
exclude: 2
gooseball not found

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
stale question

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial