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 | ||
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