5

I hope, this is not a very stupid question, but since some hours, I'm looking for a solution, but without success.

I have a table with name of cities:

cities.name

Berlin
New York
Hamburg
...

And I have a string: "Bonn New York Chicago"

Now, I would like to pick all "known" cities from this string. But as you can see, I'm not able to split this string to an array (because of "New York"). So "FIND_IN_SET" is not suitable for me. The function "LIKE" is working in the "wrong direction".

So I would need something like %cities.name% EKIL $string :-D

Does anyone have an idea, how I can handle this problem without using PHP?

I hope, I'm not missing the forest for the trees...

Thanks a lot for your help!

Tobias

4
  • This is as much a MySQL problem as a PHP one. Use WHERE name IN ('Berlin', 'New York', 'Hamburg') Commented Jan 30, 2016 at 9:51
  • 1
    @TimBiegeleisen I think the author does not know yet that the haystack string contains 'Berlin', 'New York' instead of 'Berlin New', 'York Hamburg', etc. Commented Jan 30, 2016 at 9:55
  • Foo-Bar on myself. It helps to read the whole problem. Commented Jan 30, 2016 at 9:56
  • @NoChecksum: Yes, you're right. I'm reciving an arbitrary string and I will remove everything other than letters and blanks. So everything could be possible :) Commented Jan 30, 2016 at 10:03

1 Answer 1

1

You need to make one cities array and then check your string against your cities array.

$to_match = array('Bonn','New York','Chicago'); // all cities array

$str = "Bonn New York Chicago"; // Your city string
$new_array = array();

foreach($to_match as $value) {
  if(stristr($str, $value)) {
    $new_array[] = $value;
  }
}

print_r($new_array); // here you can get only match cities against all cities array

This will iterate over each array element, then check if the value of match exists in $str, Then it will add it to $new_array.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! It seems, there is no chance to handle it with pure SQL... so, I will take your suggestion. Thanks again for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.