开发者

How do I programmatically generate an array of all permutations of a string pattern?

I need to compare a string grabbed from a URL, with a list of cities in a MySQL DB, using PHP

SELECT 'city' WHERE 'city' IN (" . $citynameArray . ") LIMIT 1;

$citynameArray is to be list of all possible permutations of $cityname, where $cityname may be up to 5 strings separated by hyp开发者_如何学Pythonhens, BUT 'city' may use either spaces OR hyphens to separate each string.

So 'city' may look like any of these:

  • string
  • string string
  • string string string
  • string string string string
  • string string string string string
  • string-string
  • string-string-string
  • string-string-string-string
  • string-string-string-string-string
  • string string-string
  • string-string string
  • string-string string string
  • string string-string-string
  • string string string-string
  • string string string-string
  • string-string-string string
  • string-string string-string
  • string-string-string string
  • string-string string string string
  • string-string-string string string
  • string-string-string-string string

... and so on, to a maximum of 5 separated strings

In the vast majority of cases however, 'city' is either:

  • string
  • string string
  • string-string

or, less common:

  • string-string string
  • string string-string

My question then, how do I generate the array? And, by using 'LIMIT 1', will the query stop looping through the array as soon as a result is found?

I appreciate any insights,

regards,

GJ


I had hoped for elegance, but this will have to do.

CASE 1 represents 90% of use-cases, and the overhead is negligable.

Percentage of use-cases depreciates to CASE 5 which applies to 1 out of 2000 cases. I analyzed the actual patterns for CASE 4, and the patterns did not have to be exhaustive.

    // FIRST, CREATE AN ARRAY OF $LocaleURL PATTERNS, TAKING INTO ACCOUNT THE VARIABLE USE OF HYPHENS

    $urlCityArray=explode("-",$LocaleURL);
    $countSegs = (count($urlCityArray)); // how many array elements

    if ($countSegs == 1) {
        $urlCityQuery = $urlCityArray[0];
    } 
    else if ($countSegs == 2) {

        $urlCityQuery  = $urlCityArray[0] . " " . $urlCityArray[1] . "', '";
        $urlCityQuery .= $urlCityArray[0] . "-" . $urlCityArray[1];

        // city1 city2, city1-city2
    }
    else if ($countSegs == 3) {

        $urlCityQuery  = $urlCityArray[0] . " " . $urlCityArray[1] . " " . $urlCityArray[2] . "', '";
        $urlCityQuery .= $urlCityArray[0] . "-" . $urlCityArray[1] . " " . $urlCityArray[2] . "', '";
        $urlCityQuery .= $urlCityArray[0] . " " . $urlCityArray[1] . "-" . $urlCityArray[2] . "', '";
        $urlCityQuery .= $urlCityArray[0] . "-" . $urlCityArray[1] . "-" . $urlCityArray[2];

        // city1 city2 city3, city1-city2 city3, city1 city2-city3, city1-city2-city3
    }
    else if ($countSegs == 4) {

        $urlCityQuery  = $urlCityArray[0] . " " . $urlCityArray[1] . " " . $urlCityArray[2] . " " . $urlCityArray[3] . "', '";
        $urlCityQuery .= $urlCityArray[0] . " " . $urlCityArray[1] . "-" . $urlCityArray[2] . " " . $urlCityArray[3] . "', '";
        $urlCityQuery .= $urlCityArray[0] . "-" . $urlCityArray[1] . "-" . $urlCityArray[2] . "-" . $urlCityArray[3] . "', '";
        $urlCityQuery .= $urlCityArray[0] . " " . $urlCityArray[1] . " " . $urlCityArray[2] . "-" . $urlCityArray[3];

        // city1 city2 city3 city4, city1 city2-city3 city4, city1-city2-city3-city4, city1 city2 city3-city4
    }
    else if ($countSegs == 5) {

        $urlCityQuery  = $urlCityArray[0] . " " . $urlCityArray[1] . " " . $urlCityArray[2] . " " . $urlCityArray[3] . " " . $urlCityArray[4] . "', '";
        $urlCityQuery .= $urlCityArray[0] . "-" . $urlCityArray[1] . "-" . $urlCityArray[2] . "-" . $urlCityArray[3] . "-" . $urlCityArray[4];

        // Notre-dame-de-lile-perrot
    }

    SELECT 'city' FROM Cities WHERE 'city' IN (" . $urlCityQuery . ") LIMIT 1;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜