String Search - how to find the "most" accurate string? Assume I have the following list of companies:


Apple
Big Apple
Google


Then I have this sample string:


  Big Apple is a company that is trying to be the next Google.


Given the list of companies and the sample string above, how can I find the list of companies in the sample string?

If I iterate through the list of companies and search for it in the sample string, I would find three matches: Apple (because "Apple" exist in "Big Apple"), Big Apple, and Google. In my scenario, I wouldn't want just Apple to match since Big Apple is a more accurate match.

I'm trying to implement the solution in PHP, but a general description/strategy on how to solve this would be helpful.
One way to do so is as follows:


Sort the company names from longest to shortest.
For each company name $c$:

a. Find all the substrings of the input string that equal $c$ and don't use any characters from a previous occurrence of a company name.

b. If some substring exists, add $c$ to the solution.


This strategy works if you're trying to solve the problem you've stated. But if you want a more robust solution, you should look into using natural language processing tools like Python's NLTK. These tools allow you to take syntax and context into account, and might help you tell the difference between "I ate a big apple", and "I bought shares in Big Apple".