基于WordPress数据库的PHP词形匹配函数问题:无匹配时正确返回用户输入值及代码优化需求
Hey there! I see you're working on a WordPress function to match Russian vocabulary variants to their prepositional case forms, and you're stuck with duplicate output when there's no match. Let's break this down and fix it, plus add some optimizations to make your code cleaner and more efficient.
The Root Cause of Duplicate Output
Your current code loops through every row in the database. If you added an else clause to echo the original input, it would run once per non-matching row—that's exactly why you're seeing repeated text. Instead, we need to set a default value (the original input) and only overwrite it if we find a match, then output it once at the end.
Fixed & Optimized Code
Here's the revised function with fixes and key improvements:
function d_typeplace_morf($d_typeplace) { global $wpdb; // Handle empty input upfront to avoid unnecessary work if (empty(trim($d_typeplace))) { return ''; } // Normalize input to lowercase once (no need to repeat this in every loop) $lowercase_input = mb_strtolower(trim($d_typeplace)); // Optimize: Let the database do the matching (faster than looping all rows in PHP) $table_name = $wpdb->prefix . 'jet_cct_tip_mest_obrabotki'; // Use prefix for portability $results = $wpdb->get_results( $wpdb->prepare( "SELECT ego_slovoforma_v_predlozhnom_padezhe FROM $table_name WHERE LOWER(vozmozhnyi_variant_mesta) LIKE %s", '%' . $wpdb->esc_like($lowercase_input) . '%' ) ); // Set default output to the original input $output = $d_typeplace; // Overwrite output if we found a matching variant if (!empty($results)) { // Use the first match (adjust this if you need to handle multiple matches) $output = $results[0]->ego_slovoforma_v_predlozhnom_padezhe; } // Return the result instead of echoing directly (more flexible for reuse) return $output; } // Example usage: // echo d_typeplace_morf("ваш ввод");
Key Improvements Explained
- No more duplicate output: We start with the original input as the default. If a match is found, we update the output value once, then return it—no repeated echoes.
- Faster database query: Instead of fetching every row and looping through them in PHP, we use a
LIKEquery to let the database find matches directly. This is way more efficient, especially as your database grows. - Input safety & portability: Used
$wpdb->prefixto make the table name work across different WordPress installs, and$wpdb->prepare/$wpdb->esc_liketo prevent SQL injection risks. - Flexible return value: The function returns the result instead of echoing it immediately, so you can use the value in other parts of your code (not just output it right away).
- Empty input handling: Added a check to skip unnecessary database queries if the input is blank.
Extra Tips for Your Russian Morphology Use Case
- Add a database index: If your
vozmozhnyi_variant_mestacolumn has lots of entries, add an index to it. This will make theLIKEquery run even faster. - Handle multiple matches: If multiple vocabulary variants can match the input, decide whether to return the first match, all matches, or implement logic to pick the best fit. The current code uses the first match.
- Case consistency: By normalizing both the input and database values to lowercase, we ensure case-insensitive matching, which is ideal for Russian word variants.
内容的提问来源于stack exchange,提问作者narivi2387

