PHP/MYSQL: تسليط الضوء على نتائج الاستعلام "Sounds Like"
-
21-09-2019 - |
سؤال
Quick MySQL/PHP سؤال. أنا أستخدم استعلام بحث "غير متخلف" كاحتفال إذا لم يتم العثور على نتائج مع استعلام بحث عادي ، لما يلي:
foreach($find_array as $word) {
clauses[] = "(firstname SOUNDS LIKE '$word%' OR lastname SOUNDS LIKE '$word%')";
}
if (!empty($clauses)) $filter='('.implode(' AND ', $clauses).')';
$query = "SELECT * FROM table WHERE $filter";
الآن ، أنا أستخدم PHP لتسليط الضوء على النتائج ، مثل:
foreach ($find_array as $term_to_highlight){
foreach ($result as $key => $result_string){
$result[$key]=highlight_stuff($result_string, $term_to_highlight);
}
}
لكن هذه الطريقة تقع على مؤخرتها عندما لا أعرف ما يجب تسليط الضوء عليه. هل هناك أي طريقة لمعرفة ما هي تطابق "الصوتي" عند تشغيل استعلام MySQL؟
وهذا يعني ، إذا كان شخص ما يبحث عن "جوان" أريد أن يسلط الضوء على "جون" بدلاً من ذلك.
المحلول
يقارن Sound Like Condition فقط مفتاح Soundex لكلا الكلمتين ، ويمكنك استخدام وظيفة PHP SoundEx () لإنشاء نفس المفتاح.
لذلك ، إذا وجدت صفًا مطابقًا وتحتاج إلى معرفة الكلمة التي يجب تسليط الضوء عليها ، فيمكنك إحضار كل من اسم FirstName و LastName ، ثم استخدام PHP للعثور على أي واحد يتطابق وتسليط الضوء على تلك الكلمة.
لقد صنعت هذا الرمز فقط لتجربة هذا. (اضطررت لاختبار نظريتي XD)
<?php
// A space seperated string of keywords, presumably from a search box somewhere.
$search_string = 'John Doe';
// Create a data array to contain the keywords and their matches.
// Keywords are grouped by their soundex keys.
$data = array();
foreach(explode(' ', $search_string) as $_word) {
$data[soundex($_word)]['keywords'][] = $_word;
}
// Execute a query to find all rows matching the soundex keys for the words.
$soundex_list = "'". implode("','", array_keys($data)) ."'";
$sql = "SELECT id, firstname, lastname
FROM sounds_like
WHERE SOUNDEX(firstname) IN({$soundex_list})
OR SOUNDEX(lastname) IN({$soundex_list})";
$sql_result = $dbLink->query($sql);
// Add the matches to their respective soundex key in the data array.
// This checks which word matched, the first or last name, and tags
// that word as the match so it can be highlighted later.
if($sql_result) {
while($_row = $sql_result->fetch_assoc()) {
foreach($data as $_soundex => &$_elem) {
if(soundex($_row['firstname']) == $_soundex) {
$_row['matches'] = 'firstname';
$_elem['matches'][] = $_row;
}
else if(soundex($_row['lastname']) == $_soundex) {
$_row['matches'] = 'lastname';
$_elem['matches'][] = $_row;
}
}
}
}
// Print the results as a simple text list.
header('content-type: text/plain');
echo "-- Possible results --\n";
foreach($data as $_group) {
// Print the keywords for this group's soundex key.
$keyword_list = "'". implode("', '", $_group['keywords']) ."'";
echo "For keywords: {$keyword_list}\n";
// Print all the matches for this group, if any.
if(isset($_group['matches']) && count($_group['matches']) > 0) {
foreach($_group['matches'] as $_match) {
// Highlight the matching word by encapsulatin it in dashes.
if($_match['matches'] == 'firstname') {
$_match['firstname'] = "-{$_match['firstname']}-";
}
else {
$_match['lastname'] = "-{$_match['lastname']}-";
}
echo " #{$_match['id']}: {$_match['firstname']} {$_match['lastname']}\n";
}
}
else {
echo " No matches.\n";
}
}
?>
يمكن أن تبدو وظيفة أكثر تعميمًا ، لسحب كلمة Soundex المطابقة من سلاسل:
<?php
/**
* Attempts to find the first word in the $heystack that is a soundex
* match for the $needle.
*/
function find_soundex_match($heystack, $needle) {
$words = explode(' ', $heystack);
$needle_soundex = soundex($needle);
foreach($words as $_word) {
if(soundex($_word) == $needle_soundex) {
return $_word;
}
}
return false;
}
?>
الذي ، إذا فهمت ذلك بشكل صحيح ، يمكن استخدامه في الكود المنشور مسبقًا على النحو التالي:
foreach ($find_array as $term_to_highlight){
foreach ($result as $key => $result_string){
$match_to_highlight = find_soundex_match($result_string, $term_to_highlight);
$result[$key]=highlight_stuff($result_string, $match_to_highlight);
}
}
لن يكون هذا بنفس الكفاءة ، مثل الكود الأكثر استهدافًا في المقتطف الأول.
نصائح أخرى
لاحظ أن SOUNDS LIKE
لا يعمل كما تعتقد. لا يعادل LIKE
في MySQL ، لأنه لا يدعم %
البرية.
هذا يعني أن استعلامك لن يجد "جون ديفيد" عند البحث عن "جون". قد يكون هذا مقبولًا إذا كان هذا مجرد احتياطي ، لكنه ليس مثاليًا.
إذن هنا اقتراح مختلف (قد يحتاج إلى تحسين) ؛ أول استخدام phps soundex()
وظيفة للعثور على Soundex للكلمة الرئيسية التي تبحث عنها.
$soundex = soundex($word);
$soundexPrefix = substr($soundex, 0, 2); // first two characters of soundex
$sql = "SELECT lastname, firstname ".
"FROM table WHERE SOUNDEX(lastname) LIKE '$soundexPrefix%' ".
"OR SOUNDEX(firstname) LIKE '$soundexPrefix%'";
الآن سيكون لديك قائمة بالأسماء الأولى والأحير التي لها تشابه غامض في السبر (قد يكون هذا الكثير من الإدخالات ، وقد ترغب في زيادة طول بادئة Soundex التي تستخدمها في البحث). يمكنك بعد ذلك حساب مسافة Levenshtein بين Soundex لكل كلمة ومصطلح البحث الخاص بك ، وفرز ذلك.
ثانياً ، يجب أن تنظر إلى الاستعلامات المعلمة في MySQL ، لتجنب بق الحانات حقن SQL.