jfcherng/php-sequence-matcher
PHP 8.4+ longest sequence matcher inspired by Python difflib. Compare strings or arrays to find matching blocks and similarities for diffing, change detection, and text analysis. Lightweight library extracted from php-diff with improvements.
SequenceMismatch) via Laravel’s event system for reactive applications.diff2html) for human-readable diffs.app()->bind() or facades.PHPUnit for isolated testing.SequenceMatcher methods or inject configuration via Laravel’s service container.saved) to compare field changes dynamically.cache()->remember for repeated comparisons (e.g., API response validation).difflib for consistency.)cache()->forever)?Levenshtein for short texts + this package for long sequences) work?php-diff, custom implementations)?composer require jfcherng/php-sequence-matcher).$this->app->singleton(SequenceMatcher::class, fn() => new \Jfcherng\SequenceMatcher\SequenceMatcher());
SequenceMatcher::compare() facade for concise usage.public function getSimilarityAttribute()
{
return app(SequenceMatcher::class)->ratio($this->content, $this->original_content);
}
public function scopeSimilarTo($query, $reference)
{
return $query->whereRaw("similarity > ?", [config('sequence-matcher.threshold')]);
}
{ "similarity": 0.85, "matches": ["field1", "field2"] }
php artisan sequence:compare posts/1 posts/2
SequenceMatcher::ratio()).difflib for edge cases (e.g., Unicode, mixed data types).Levenshtein (via rubix/ml).difflib).__toString() or toArray() adapters.Str::lower() for consistent comparisons.Carbon::parse() for datetime comparisons.composer require jfcherng/php-sequence-matcher
AppServiceProvider):
public function register()
{
$this->app->singleton(SequenceMatcher::class);
}
$ratio = app(SequenceMatcher::class)->ratio("Laravel", "Laravul"); // ~0.857
class Post extends Model
{
public function getSimilarityAttribute()
{
return app(SequenceMatcher::class)->ratio($this->content, $this->original_content);
}
}
PHPUnit or use Laravel’s partialMock."café" vs. "cafe").composer update.roave/security-advisories to track PHP vulnerabilities.config/sequence-matcher.php:
'threshold' => 0.7, // Minimum ratio to consider a "match"
'cache_enabled' => env('SEQUENCE_MATCHER_CACHE', false),
SequenceMatcher for custom scoring:
class CustomMatcher extends \Jfcherng\SequenceMatcher\SequenceMatcher
{
public function getOpCodes(): array { /* Custom logic */ }
}
Xdebug to identify bottlenecks (e.g., large array comparisons).difflib:
How can I help you explore Laravel packages today?