Issues with search query to match values

Can anyone tell me how I could do the following please.
I need to pull corresponding id values from a table based on a model name. The problem I have is that the model value I have holds more data than the model name row in the table does.

For example:

$model_name = MiTo Hatchback 3-Door

The $model_name in the table that corresponds to this is just MiTo

How can I set up a script that will recognise these as being the same because they both contain the word MiTo?

Any help much appreciated. Thanks in advance.

answer the question with a question, thats my policy.

Is the first word always going to be the one you’re trying to find?

For this part anyway yes it will be. Do I need to use something like an Explode based on spaces?

That’d be my first step, yup. search term is the first (zero) element of your exploded…ness.

I’ve tried the following and its returning nothing!! What’s wrong with the code?

$model_term = explode(’ ‘, $row[‘Model’]);
$querymodel = ‘SELECT Make_Id,Model_Id FROM My_Table WHERE Model_Name LIKE "%’.$model_term[0].’%"';

ignore my last comment, think its working now. hold on.

Extract the model name from the string and use it in your query. If it is guaranteed that your model name does not contain spaces then you can use php’s explode function like this:

<?php
$model_name = "MiTo Hatchback 3-Door";
list($model,) = explode(" ", $model_name);
echo $model;
?>

If your $model_name contains something that cannot be handled via explode, you can use regular expressions or preg_split.