0

I am building small application which has an input for a string. I also have an array of words and I want to match if any full value in the array matches partially with the input string. Example:

Array('London Airport', 'Mancunian fields', 'Disneyland Florida') 

If a user types 'Disneyland Florida in USA' or just 'Disneyland Florida, USA' I want to return a match.

Any help would be highly appreciated. Thanks in advance.

2
  • 1
    What have you already tried? Commented Jul 29, 2013 at 17:16
  • I have already tried the in_array which only returns true for full string match Commented Jul 29, 2013 at 17:20

3 Answers 3

1

Data to search in:

<?php
$data = array(
    0 => 'London Airport', 
    1 => 'Mancunian fields', 
    2 => 'Disneyland Florida'
);

Find full string

Searching function:

<?php
/**
 * @param array $data
 * @param string $what
 * @return bool|string
 */
function searchIn($data, $what) {
    foreach ($data as $row) {
        if (strstr($what, $row)) {
            return $row;
        }
    }

    return false;
}

Results:

<?php
// Disney Florida
echo searchIn($data, 'Disneyland Florida in USA');

// Disney Florida
echo searchIn($data, 'Disneyland Florida, USA');

// false
echo searchIn($data, 'whatever Florida Disneyland');
echo searchIn($data, 'No match');
echo searchIn($data, 'London');

Find by any combination of words

Searching function:

<?php
/**
 * @param array $data
 * @param string $what
 * @return int
 */
function searchIn($data, $what) {
    $needles = explode(' ', preg_replace('/[^A-Za-z0-9 ]/', '', $what));

    foreach ($data as $row) {
        $result = false;

        foreach ($needles as $needle) {
            $stack = explode(' ', $row);

            if (!in_array($needle, $stack)) {
                continue;
            }

            $result = $row;
        }

        if ($result !== false) {
            return $result;
        }
    }

    return false;
}

Results:

<?php
// Disneyland Florida
echo searchIn($data, 'Disneyland Florida in USA');

// Disneyland Florida
echo searchIn($data, 'Disneyland Florida, USA');

// Disneyland Florida
echo searchIn($data, 'whatever Florida Disneyland');

// false
echo searchIn($data, 'No match');

// London Airport
echo searchIn($data, 'London');

As you can see, id doesn't matter in what order user is searching and whether or not the string starts with Disneyland.

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

4 Comments

Thanks for your time and solution. Your solution matches any keyword and what I wanted is only a complete match from the array. i.e if user enters "Disneyland Florida" or "Disneyland Florida, USA" both cases to return true but if user enters "USA Disneyland NOT Florida" or "Florida Disneyland" both cases to return false.
So, I used a cannon to kill a fly ;) The inputs which should return false: Florida Disneyland, Something Disneyland Florida, Disneyland Something Florida, Disneyland. Right?
correct. only if full string on the array data is matched with either partial or full string of the user input should return true. Once again thank you for your time.
That did it. I honestly could not thank you enough. I appreciate your help.
0
function isInExpectedPlace($inputPlace) {
    $places = array('London Airport', 'Mancunian fields', 'Disneyland Florida');
    foreach($places as $place) {
        if(strpos($inputPlace, $place) !== false)
            return true;
        }
    }
    return false;
}

Comments

0

PHP 5.3+ for the use of the anonymous function:

<?php

$places = array('London Airport', 'Mancunian fields', 'Disneyland Florida');
$search = 'Disneyland Florida in USA';

$matches = array_filter($places, function ($place) use ($search) {
    return stripos($search, $place) !== false;
});

var_dump($matches);

Comments

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.