kwi/urllinker
Laravel/PHP URL linker that scans text and converts web addresses, emails, and similar patterns into clickable HTML links. Lightweight helper for turning plain content into safe, formatted output in views, notifications, and user-generated text.
rel="nofollow").autolinker.js).embed/embed or dedicated APIs).UrlLinker via Facade for clean syntax (e.g., UrlLinker::link()).@linkUrls directive for templates.prepareForValidation().Purifier-cleaned content) but requires caution in user-submitted HTML to avoid XSS.| Risk | Mitigation Strategy |
|---|---|
| Regex Overmatching | Test with edge cases (e.g., example.com in code blocks, Unicode URLs). |
| Performance at Scale | Benchmark with large texts (e.g., 10K+ characters); cache results if repeated. |
| HTML Injection | Always sanitize output with Purifier or strip_tags() for untrusted HTML contexts. |
| PHP Version Compatibility | Test on PHP 8.1+; fork if deprecations arise (e.g., preg_replace changes). |
| False Negatives | Configure strict regex or post-process with filter_var($url, FILTER_VALIDATE_URL). |
file_get_contents($url, null, stream_context_create(['http' => ['timeout' => 1]]))?Purifier or other filters?[invalid URL])?UrlLinker as a singleton for dependency injection.UrlLinker facade for concise syntax.@linkUrls for templates.prepareForValidation().new \Kwi\UrlLinker\UrlLinker();.composer require kwi/urllinker.use Kwi\UrlLinker\UrlLinker;
echo UrlLinker::link("Visit http://example.com");
// app/Providers/UrlLinkerServiceProvider.php
public function register() {
$this->app->singleton(UrlLinker::class);
$this->app->alias(UrlLinker::class, 'urllinker');
}
Usage:
$linkedText = app(UrlLinker::class)->link($comment->text);
// app/Facades/UrlLinker.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class UrlLinker extends Facade { protected static function getFacadeAccessor() { return 'urllinker'; } }
Usage:
use App\Facades\UrlLinker;
$linkedText = UrlLinker::link($text);
// app/Providers/BladeServiceProvider.php
Blade::directive('linkUrls', function ($expression) {
return "<?php echo \\Kwi\\UrlLinker\\UrlLinker::link({$expression}); ?>";
});
Usage in Blade:
{!! linkUrls($comment->text) !!}
Purifier for HTML contexts:
use Purifier;
$cleanHtml = Purifier::clean(UrlLinker::link($text));
https://例子.测试).<code> blocks).null, empty strings, or non-string inputs.preg_replace).error_log("Input: {$text} | Output: {$linkedText}");
dd() to inspect intermediate outputs.http://) to avoid false links.safeLink() wrapper that escapes HTML on failure:
public static function safeLink($text) {
$linked = self::link($text);
return htmlspecialchars($linked, ENT_QUOTES, 'UTF-8');
}
$cacheKey = md5($text);
$linkedText = Cache::remember($cacheKey, now()->addHours(1), function() use ($text) {
return UrlLinker::link($text);
});
UrlLinker::dispatch($text)->onQueue('linking');
| Scenario | Impact | Mitigation |
|---|---|---|
| Regex Overmatching | Links non-URL text (e.g., code). |
Tune regex or post-validate with filter_var(). |
| HTML Injection | XSS if used in unsanitized HTML. | Always sanitize output (e.g., Purifier). |
| Performance Bottleneck | Slow for large texts/batches. | Add rate limiting or caching. |
| PHP Deprecation | Breaks on PHP 9.x. | Fork and update regex syntax. |
How can I help you explore Laravel packages today?