PHP String Differences and Dynamic Restrictions

前端 未结 1 506
花落未央
花落未央 2021-01-21 14:09

Example A [the simplified one]: --------------------------------------------------------------------

MODEL: String

相关标签:
1条回答
  • 2021-01-21 14:39

    when Christopher Johnson McCandless is mapped to {1}{2}:

    possible combination for forming two groups is:

    • Christopher Johnson and McCandless
    • Christopher and Johnson McCandless

    when cinema tomorrow at night is mapped to {3}{4}

    possible combination for forming two groups is:

    • cinema and tomorrow at night
    • cinema tomorrow and at night
    • cinema tomorrow at and night

    Write a PHP function to get_possible_groups($string_of_words, $group_count) returns array of array of group combinations.

    and an SQL statement like:

    SELECT count(*), 'cinema' firstWordGroup, 'tomorrow at night' secondWordGroup
      FROM possibleMatchTable
     WHERE possible_match IN ('cinema', 'tomorrow at night')
    UNION
    SELECT count(*), 'cinema tomorrow', 'at night'
      FROM possibleMatchTable
     WHERE possible_match IN ('cinema tomorrow', 'at night')
    UNION
    SELECT count(*), 'cinema tomorrow at', 'night'
      FROM possibleMatchTable
     WHERE possible_match IN ('cinema tomorrow at', 'night');
    

    one possible output can be:

    +----------+--------------------+-------------------+
    | count(*) | firstWordGroup     | secondWordGroup   |
    +----------+--------------------+-------------------+
    |        2 | cinema             | tomorrow at night |
    |        0 | cinema tomorrow    | at night          |
    |        0 | cinema tomorrow at | night             |
    +----------+--------------------+-------------------+
    

    whichever has count 2(two word groups) that's your answer.

    If MODEL text is a fulltext indexed column then for any given random string you can get most relevant model like:

    SELECT * FROM model_strings 
    WHERE MATCH(model) AGAINST ('Damn you Spar, Kot will kill you.');
    

    query might return you something like:

    +----------------------------------+
    | model                            |
    +----------------------------------+
    | Damn you {1}, {2} will kill you. |
    +----------------------------------+
    

    Extracting the words for random string using placeholders from Model:

    <?php 
    
    $placeholder_pRegEx = '#\{\d+\}#';
    
    $model = 'Damn you {1}, {2} will kill you. {3}{4}{5}';
    $string = 'Damn you Spar, Will will kill you. I Love it man.';
    
    $model_words = explode(' ', $model);
    $string_words = explode(' ', $string);
    
    $placeholder_words = array();
    
    for ($idx =0, $jdx=0; $idx < count($string_words); $idx ++) {
    
        if ($jdx < count($model_words)) {
            if (strcmp($string_words[$idx], $model_words[$jdx])) {
                $placeholder_words[] = $string_words[$idx];
    
                //Move to next word in Model only if it's a placeholder
                if (preg_match($placeholder_pRegEx, $model_words[$jdx]))
                    $jdx++;
    
            } else
                $jdx++; //they match so move to next word
        } else
            $placeholder_words[] = $string_words[$idx];
    }
    
    //Even status will have the count
    $status = preg_match_all ($placeholder_pRegEx, $model, $placeholders);
    
    $group_count = count($placeholders[0]);
    
    var_dump(get_defined_vars());
    ?>
    

    Above code will get you values like:

    'placeholder_words' => array (size=6)
      0 => string 'Spar,' (length=5)
      1 => string 'Will' (length=4)
      2 => string 'I' (length=1)
      3 => string 'Love' (length=4)
      4 => string 'it' (length=2)
      5 => string 'man.' (length=4)
    
    'placeholders' => array (size=1)
      0 => 
        array (size=5)
          0 => string '{1}' (length=3)
          1 => string '{2}' (length=3)
          2 => string '{3}' (length=3)
          3 => string '{4}' (length=3)
          4 => string '{5}' (length=3)
    
    'group_count' => int 5
    
    • from there you can call get possible groupings
    • then SQL query to check against allowed possible matches
    • actual words in required groupings.

    Alas, it's some question, eh!

    0 讨论(0)
提交回复
热议问题