når Christopher Johnson McCandless
er knyttet til {1}{2}
:
mulig kombination for at danne to grupper er:
Christopher Johnson
ogMcCandless
Christopher
ogJohnson McCandless
når cinema tomorrow at night
er knyttet til {3}{4}
mulig kombination for at danne to grupper er:
cinema
ogtomorrow at night
cinema tomorrow
ogat night
cinema tomorrow at
ognight
Skriv en PHP-funktion tilget_possible_groups($string_of_words, $group_count)
returnerer matrix af matrix af gruppekombinationer.
og en SQL-sætning som:
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');
et muligt output kan være:
+----------+--------------------+-------------------+
| count(*) | firstWordGroup | secondWordGroup |
+----------+--------------------+-------------------+
| 2 | cinema | tomorrow at night |
| 0 | cinema tomorrow | at night |
| 0 | cinema tomorrow at | night |
+----------+--------------------+-------------------+
alt efter hvad der har tæller 2 (to ordgrupper), det er dit svar.
Hvis MODEL
tekst er en fulltext
indekseret kolonne, så kan du for enhver given tilfældig streng få den mest relevante model som:
SELECT * FROM model_strings
WHERE MATCH(model) AGAINST ('Damn you Spar, Kot will kill you.');
forespørgsel kan returnere dig noget som:
+----------------------------------+
| model |
+----------------------------------+
| Damn you {1}, {2} will kill you. |
+----------------------------------+
Udtræk ordene for tilfældig streng ved hjælp af pladsholdere fra 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());
?>
Ovenstående kode vil give dig værdier som:
'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
- derfra kan du ringe til
get possible groupings
- derefter SQL-forespørgsel for at kontrollere mod tilladte mulige match
- faktiske ord i obligatoriske grupperinger.
Ak, det er et spørgsmål, eh!