Shopify and WooCommerce both let customers leave order notes β but most merchants struggle to search, filter, or organize them efficiently. What if you could auto-tag notes with labels like:
- π Gift
- π Birthday
- π¨ Urgent
β¦even when customers write typos like βgfitβ or use slang like βasapβ?
Welcome to fuzzy matching β a simple way to intelligently detect keywords in messy text using native PHP or open-source libraries.
π§© What is Fuzzy Matching?
Fuzzy matching compares two strings and checks how similar they are β even if theyβre not exactly the same.
Unlike strict string matching (if ($text == "urgent")
), fuzzy matching lets you catch:
- Typos:
brithday
βbirthday
- Short forms:
bday
βbirthday
- Variants:
gifting
βgift
π οΈ Use Case: Auto-Tag Orders Based on Note Content
Letβs say a customer writes:
βPlease wrap this as a gfit. Itβs for a brithday surprise. ASAP!β
Your app should auto-tag it as:
- π Gift
- π Birthday
- π¨ Urgent
β Option 1: Pure PHP (No Libraries)
You can use PHPβs native levenshtein()
function β it returns the number of edits (insertions, deletions, swaps) between two strings.
function fuzzy_match($word, $keyword, $threshold = 2) {
return levenshtein(strtolower($word), strtolower($keyword)) <= $threshold;
}
$note = "Please wrap this as a gfit. It's a brithday gift!";
$keywords = ['gift', 'urgent', 'birthday'];
$words = preg_split('/[\s,.!?]+/', strtolower($note));
foreach ($keywords as $keyword) {
foreach ($words as $word) {
if (fuzzy_match($word, $keyword)) {
echo "Matched: $word β $keyword\n";
}
}
}
π’ Output:
Matched: gfit β gift
Matched: brithday β birthday
You can tweak the
$threshold
to be more or less strict.
β Option 2: Using a Composer Library (More Accurate)
For more advanced scoring, use jfcherng/php-fuzzy-search
.
composer require jfcherng/php-fuzzy-search
use Jfcherng\FuzzySearch\FuzzySearch;
$note = "I need this ASAP! It's for a birthday surprise.";
$keywords = ['gift', 'urgent', 'birthday'];
$words = preg_split('/[\s,.!?]+/', strtolower($note));
$matches = [];
foreach ($keywords as $keyword) {
foreach ($words as $word) {
$score = FuzzySearch::score($word, $keyword); // 0 - 100
if ($score >= 80) {
$matches[] = [
'word' => $word,
'matched' => $keyword,
'score' => $score,
];
}
}
}
foreach ($matches as $match) {
echo "Matched '{$match['word']}' β '{$match['matched']}' (score: {$match['score']})\n";
}
π’ Output:
Matched 'asap' β 'urgent' (score: 84)
Matched 'birthday' β 'birthday' (score: 100)
π§ Bonus: Synonym Groups
Define your own keyword groups so the app can detect multiple versions of a concept.
$tagGroups = [
'gift' => ['gift', 'gifting', 'gfit'],
'urgent' => ['urgent', 'asap', 'rush'],
'birthday' => ['birthday', 'bday', 'brithday']
];
foreach ($tagGroups as $tag => $variants) {
foreach ($variants as $variant) {
foreach ($words as $word) {
if (fuzzy_match($word, $variant)) {
echo "Apply tag: $tag (matched '$word')\n";
}
}
}
}
π― Summary
Feature | Native PHP | Composer Library |
---|---|---|
Typos | β | β |
Similar words | β οΈ (basic) | β (accurate) |
Scores / Ranking | β | β |
Setup | Easy | Needs Composer |
If you’re building a Shopify/WooCommerce plugin or internal order management tool, fuzzy matching brings smart automation without AI overhead.
π Want More?
In the next post, Iβll show how to:
- Save auto-tag rules in a dashboard
- Create a visual tag rule builder (like Gmail filters)
- Send alerts via email/Slack for urgent order notes
Leave a Reply