khi Christopher Johnson McCandless được ánh xạ tới {1}{2} :
sự kết hợp có thể có để tạo thành hai nhóm là:
-
Christopher JohnsonvàMcCandless -
ChristophervàJohnson McCandless
khi cinema tomorrow at night được ánh xạ tới {3}{4}
sự kết hợp có thể có để tạo thành hai nhóm là:
-
cinemavàtomorrow at night -
cinema tomorrowvàat night -
cinema tomorrow atvànight
Viết một hàm PHP vào get_possible_groups($string_of_words, $group_count) trả về mảng mảng kết hợp nhóm.
và một câu lệnh SQL như:
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');
một đầu ra khả thi có thể là:
+----------+--------------------+-------------------+
| count(*) | firstWordGroup | secondWordGroup |
+----------+--------------------+-------------------+
| 2 | cinema | tomorrow at night |
| 0 | cinema tomorrow | at night |
| 0 | cinema tomorrow at | night |
+----------+--------------------+-------------------+
cái nào có số lượng 2 (hai nhóm từ) thì đó là câu trả lời của bạn.
Nếu MODEL văn bản là một fulltext cột được lập chỉ mục sau đó đối với bất kỳ chuỗi ngẫu nhiên nhất định nào, bạn có thể nhận được mô hình phù hợp nhất như:
SELECT * FROM model_strings
WHERE MATCH(model) AGAINST ('Damn you Spar, Kot will kill you.');
truy vấn có thể trả lại cho bạn một cái gì đó như:
+----------------------------------+
| model |
+----------------------------------+
| Damn you {1}, {2} will kill you. |
+----------------------------------+
Trích xuất các từ cho chuỗi ngẫu nhiên bằng cách sử dụng trình giữ chỗ từ 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());
?>
Đoạn mã trên sẽ cho bạn các giá trị như:
'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
- từ đó, bạn có thể gọi
get possible groupings - sau đó truy vấn SQL để kiểm tra các kết quả phù hợp có thể được phép
- các từ thực tế trong các nhóm bắt buộc.
Chao ôi, đó là một số câu hỏi, ơ!