PHP智能文本片段高亮函数开发技术求助
Solution for Inflected Text Highlighting in PHP
Got it, let's solve this problem. The key issue with your original code is that it only does exact substring matches—we need a way to handle word inflections (like marry → married, mediate → mediated) while preserving the original text and wrapping matches in <b> tags.
Approach
We'll use regular expressions to target common verb inflection patterns (present tense, past tense, present participle, third-person singular) and build a dynamic pattern from your search phrase. Then we'll use preg_replace_callback to safely wrap matching segments in bold tags.
Working Code
function highlightInflectedPhrase(string $text, string $search): string { // Trim and split search phrase into individual words $searchWords = preg_split('/\s+/', trim($search)); if (empty($searchWords)) { return $text; } $patternParts = []; foreach ($searchWords as $word) { // Escape special regex characters in the search word $escapedWord = preg_quote($word, '/'); // Build regex part to match common inflections if (preg_match('/y$/', $escapedWord)) { // Handle words ending in y (e.g., marry → married, marrying) $stem = substr($escapedWord, 0, -1); $patternParts[] = "$stem(y|i(es|ed|ing))"; } elseif (preg_match('/e$/', $escapedWord)) { // Handle words ending in e (e.g., mediate → mediated, mediating) $stem = substr($escapedWord, 0, -1); $patternParts[] = "$stem(e|es|ed|ing)"; } else { // Handle regular words (e.g., walk → walks, walked, walking) $patternParts[] = "$escapedWord(s|ed|ing)?"; } } // Combine parts into a full regex pattern (case-insensitive, word boundaries) $pattern = '/\b(' . implode('\s+', $patternParts) . ')\b/i'; // Replace matches with bold-wrapped text return preg_replace_callback($pattern, function($matches) { return '<b>' . $matches[0] . '</b>'; }, $text); } // Test your examples echo highlightInflectedPhrase("Regulation is mediated via many different mechanisms", "mediate via") . "\n"; // Output: Regulation is <b>mediated via</b> many different mechanisms echo highlightInflectedPhrase("is mediated via many", "mediate via") . "\n"; // Output: is <b>mediated via</b> many echo highlightInflectedPhrase("are meddling in local affairs.", "meddle in") . "\n"; // Output: are <b>meddling in</b> local affairs. echo highlightInflectedPhrase("who can not get married in France.", "marry in") . "\n"; // Output: who can not get <b>married in</b> France.
How It Works
- Split Search Phrase: We break your search string into individual words to handle each term's inflections separately.
- Build Inflection Patterns:
- For words ending in
y(likemarry), we match both the original form and inflections whereybecomesi(e.g.,married,marrying). - For words ending in
e(likemediate), we match the original form plus inflections that drop thee(e.g.,mediating) or keep it (e.g.,mediated). - For regular words, we match the original form plus common suffixes (
s,ed,ing).
- For words ending in
- Regex Matching: We use word boundaries (
\b) to ensure we match full words, not substrings, and case insensitivity (i) to handle capitalization differences. - Safe Replacement:
preg_replace_callbacklets us wrap the exact matched text (preserving original case/spelling) in<b>tags.
Limitations & Improvements
- This handles common regular verb inflections but won't work for irregular verbs (e.g.,
go→went,eat→eaten). For those, you'd need a more advanced solution using natural language processing tools (like a stemming library or WordNet integration). - If your text contains HTML tags, you might want to add a check to avoid wrapping tags themselves (the current code will ignore tags since we're matching word boundaries, but if tags are inside words, you'd need additional handling).
内容的提问来源于stack exchange,提问作者MaximShatohin
相关产品推荐
相关产品推荐

