Reputation: 11
i have a Table in MySql with Cities.
|---------------------|------------------|
| ID | City |
|---------------------|------------------|
| 1 | Berlin |
|---------------------|------------------|
| 2 | Los Angeles |
|---------------------|------------------|
| 3 | Shanghai |
|---------------------|------------------|
and i have a String in Php like this: $string = "I like Berlin and Los Angeles."
Now i want look in the MySql-Database if the String contains a City/Cities. But i dont know what is the best solution. Maybe its better or/and faster to have the List with Cities as Json File?
Upvotes: 1
Views: 2308
Reputation: 2007
You an use mysql query for checking it.
Select * from table_name where match(City) against ('I like Berlin and Los Angeles')
or you can use like
Select * from table_name where city like '%I like Berlin and Los Angeles%'
I am modified the query as you need the sort mechanism in it.
Select *,(match(City) against ('I like Los Angeles and Berlin')) as relevance from city
where match(City) against ('I like Los Angeles and Berlin') ORDER BY relevance ASC;
It would give you sort on city present in table.
Upvotes: 3
Reputation: 940
You should take all cities from database to an array then checking it.
$cities = ['Berlin', 'Los Angeles', 'Shanghai'];
$string = 'I like Berlin and Los Angeles.';
$result = [];
$pattern = '/('.implode('|', $cities).')/';
preg_match_all($pattern, $string, $result);
SAMPLE: http://phpio.net/s/2t70
Upvotes: 0