1

I would like to know if there is a fast way to get a row like this:

Name (Database comumn) would be a database like |ID|Name|Age|Gender|

Not in table:

Text (long string)

Names:

John Smith
Jack Stone
Brian Woods
Text: "Someone was going to go to John Smith but instead went to Brian Woods. Why would he do that to John Smith?"

And the query should return "John Smith" matches 2 times, "Brian Woods" matches 1 time

How do you get a thing that does this fast?

$text = "Someone was going to go to John Smith but instead went to Brian Woods. Why would he do that to John Smith?";
$query = $mysql("SELECT * FROM persons");
while($r = mysql_fetch_assoc($query))
{
if(preg_match("/".$r["Name"]."/", $text))
{
$matches[$r["name"]]+=1;
}
}
print_r($matches);
My Name
  • 133
  • 7

2 Answers2

0

Are you looking for the like command?

SELECT COUNT(*)
FROM persons
WHERE name like '%John Smith%';

If you start to get a lot of data (and 3000 rows is not a lot of data), you might want to look into full text indexing.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

Depending on how long your sentence is (compared to the amount of Names you are searching for you can flip the search direction within the following method)

You can convert your sentence into an array of words (strings). Now loop through the words, concatenating each with the next and search with in_array() within the name list from your database.

Community
  • 1
  • 1
Daniel
  • 3,541
  • 3
  • 33
  • 46