iHaveacomputer
iHaveacomputer

Reputation: 1437

Find a possible substring in another string with mysql

What is the best way to find a possible substring in another string, using MySQL and/or PHP?

I have script to go trough my list of emails, and use PHP to score, categorize and display them based on my calculated priority. The tricky part is that i have a list of topics in my database i need to check, and i want to match topics from the email-subject against my 'topics' table.

To keep it simple let's say a subject is "LAST REMINDER FOR 2012 FIRST QUARTER DRAWS (OFFICIAL RELEASE).", and in my database i have a matching topic "first quarter draws". What would be the best method to find this record?

Is there a MySQL function for this; or how would you do this? I can only think of manually splitting the subject line into words and run a query against each word and then sort by the id with the most matches; but it seems rather complicated to me and there should be a better solution, or something already solved with a MySQL function.

I already had a look at the "Questions with similar titles" titles here, but the don't help me because they assume that my substring is known.

Upvotes: 0

Views: 1955

Answers (2)

Tales
Tales

Reputation: 1923

Try this:

if(substr_count( strtolower( $subject ), strtolower( $topic ) ) > 0){
 return true;
}
return false;

Upvotes: -1

Ben Rowe
Ben Rowe

Reputation: 28691

A like condition should solve this problem:

SELECT * FROM table WHERE field LIKE '%first quarter draws%'

Upvotes: 2

Related Questions