0

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.

2 Answers 2

2

A like condition should solve this problem:

SELECT * FROM table WHERE field LIKE '%first quarter draws%'
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, but this is wrong and does not answer my question. I am aware of the basic mysql sytax; however the main issue here is that i don't know the substring 'first quarter draws' (yet). I only have my subject line "LAST REMINDER FOR 2012 FIRST QUARTER DRAWS (OFFICIAL RELEASE).", and a table with ~6000 records that may, or may not(!) contain the record 'first quarter draws'. How do i find the best match?
Sorry, I'm having trouble trying to understand your question. Re-reading I think you're after full text match, and sorting by relevance: devzone.zend.com/26/using-mysql-full-text-searching you could add a FULLTEXT index to your topic field and compare it to your $subject.
-1

Try this:

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

2 Comments

This is really unnecessary. It could only be done after fetching everything and there is a function for it: us3.php.net/manual/en/function.strpos.php
Yeah, this wont help either. See my question: "... 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."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.