47: Move Phonetic Matching Out of Core
Overview
Section titled “Overview”Isolate a phonetic encoder behind a callable so you can replace the deprecated metaphone() implementation and verify your search behavior.
Prerequisites
Section titled “Prerequisites”- PHP 8.6 CLI. Confirm the version with
php --version. - Review the previous chapter if you’re following the series in order.
Allow about 10 minutes for the walkthrough and exercise.
What You’ll Build
Section titled “What You’ll Build”A runnable metaphone-migration.php example.
Step 1: Run the example (~5 min)
Section titled “Step 1: Run the example (~5 min)”Run the following from the root of the Code with PHP repository.
php code/php-8-6/47-metaphone-migration/metaphone-migration.php<?php
declare(strict_types=1);
function groupNames(array $names, callable $encode): array{ $groups = []; foreach ($names as $name) { $groups[$encode($name)][] = $name; } return $groups;}
$names = ['Smith', 'Smyth', 'Jones'];echo json_encode(groupNames($names, soundex(...)), JSON_THROW_ON_ERROR), PHP_EOL;Expected Result
Section titled “Expected Result”{"S530":["Smith","Smyth"],"J520":["Jones"]}Why It Works
Section titled “Why It Works”Persisted phonetic keys depend on the chosen algorithm, its input normalization, and its version. The demo uses the built-in English-oriented soundex() to show the callable boundary. Choose your userland encoder with representative fixtures before generating replacement search keys.
Exercise
Section titled “Exercise”Add two spelling variants from your own dataset. Record whether each algorithm groups them together and keep the result as a migration fixture.
Wrap-up
Section titled “Wrap-up”Continue with Replace Deprecated MySQLi Charset and Statement Calls.