vstelmakh/url-highlight
Composer 安装命令:
composer require vstelmakh/url-highlight
包简介
Library to parse urls from string input
README 文档
README
Url highlight - PHP library to parse URLs from string input. Works with complex URLs, edge cases and encoded input.
Features:
- Replace URLs in string by HTML tags (make clickable)
- Match URLs without scheme by top-level domain
- Work with HTML entities encoded input
- Extract URLs from string
- Check if string is URL
Installation
Install the latest version with Composer:
composer require vstelmakh/url-highlight
Also, there are
Twig extension
and
Symfony bundle available.
Quick start
<?php require __DIR__ . '/vendor/autoload.php'; use VStelmakh\UrlHighlight\UrlHighlight; $urlHighlight = new UrlHighlight(); echo $urlHighlight->highlightUrls('Hello, http://example.com.'); // Output: // Hello, <a href="http://example.com">http://example.com</a>.
💡 Tip: For customizing highlight see Highlighter. To properly handle HTML entity escaped string see Encoder.
Usage
Check if string is URL
<?php $urlHighlight->isUrl('http://example.com'); // return: true $urlHighlight->isUrl('Other string'); // return: false
Parse URLs from string
<?php $urlHighlight->getUrls('Hello, http://example.com.'); // return: ['http://example.com']
Replace URLs by HTML tags (make clickable)
<?php $urlHighlight->highlightUrls('Hello, http://example.com.'); // return: 'Hello, <a href="http://example.com">http://example.com</a>.'
Configuration
There are 3 parts which could be configured according to your needs:
- Validator - define if match is valid and should be recognized as URL (e.g. allow/disallow specific schemes)
- Highlighter - define the way how URL should be highlighted (e.g. replaced by html
<a>tag) - Encoder - define how to work with encoded input (e.g. html special chars)
Configuration provided via constructor implementing corresponding interface instance.
Use null to keep default:
<?php use VStelmakh\UrlHighlight\Encoder\HtmlSpecialcharsEncoder; use VStelmakh\UrlHighlight\UrlHighlight; use VStelmakh\UrlHighlight\Validator\Validator; $validator = new Validator(); $encoder = new HtmlSpecialcharsEncoder(); $urlHighlight = new UrlHighlight($validator, null, $encoder);
Validator
There is one validator bundled with the library. Which is used by default with settings listed in example below.
🛠️ Validator usage example
<?php use VStelmakh\UrlHighlight\UrlHighlight; use VStelmakh\UrlHighlight\Validator\Validator; $validator = new Validator( true, // bool - if should use top level domain to match urls without scheme [], // string[] - array of blacklisted schemes [], // string[] - array of whitelisted schemes true // bool - if should match emails (if match by TLD set to "false" - will match only "mailto" urls) ); $urlHighlight = new UrlHighlight($validator);
💡 Tip: If you need custom behavior - create and use your own validator implementing ValidatorInterface.
Highlighter
There are 2 highlighters bundled with the library:
-
HtmlHighlighter - convert matches to html tags.
Example:http://example.com→<a href="http://example.com">http://example.com</a> -
MarkdownHighlighter - convert matches to markdown format.
Example:http://example.com→[http://example.com](http://example.com)
By default, HtmlHighlighter is used, with settings listed in example below.
🛠️ Highlighter usage example
<?php use VStelmakh\UrlHighlight\Highlighter\HtmlHighlighter; use VStelmakh\UrlHighlight\UrlHighlight; $highlighter = new HtmlHighlighter( 'http', // string - scheme to use for urls matched by top level domain [], // string[] - key/value map of tag attributes, e.g. ['rel' => 'nofollow', 'class' => 'light'] '', // string - content to add before highlight: {here}<a... '' // string - content to add after highlight: ...</a>{here} ); $urlHighlight = new UrlHighlight(null, $highlighter);
💡 Tip: If you need custom behavior - extend HtmlHighlighter or implement HighlighterInterface.
For more details and examples see 🖍️ Custom highlighter.
Encoder
Encoder should be used to handle encoded input properly. For example HTML escaped string could contain something
like: http://example.com?a=1" or http://example.com?a=1&b=2 which will be wrongly matched as URL.
By default, there is no encoder used. There are 2 encoders bundled with library:
- HtmlEntitiesEncoder - to work with HTML entities encoded string (any character expected to be HTML entity encoded)
- HtmlSpecialcharsEncoder - to work with HTML escaped string (only
&"'<>expected to be encoded)
🛠️ Encoder usage example
<?php use VStelmakh\UrlHighlight\Encoder\HtmlSpecialcharsEncoder; use VStelmakh\UrlHighlight\UrlHighlight; $encoder = new HtmlSpecialcharsEncoder(); $urlHighlight = new UrlHighlight(null, null, $encoder); $urlHighlight->highlightUrls('<a href="http://example.com">Example</a>'); // return: '<a href="<a href="http://example.com">http://example.com</a>">Example</a>'
💡 Tip: For custom behavior - create and use your own encoder implementing EncoderInterface.
Keep in mind - using encoder require more regex operations and could have performance impact. Better to not use encoder if you don't expect encoded string.
Credits
Volodymyr Stelmakh
Licensed under the MIT License. See LICENSE for more information.
vstelmakh/url-highlight 适用场景与选型建议
vstelmakh/url-highlight 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 962.68k 次下载、GitHub Stars 达 102, 最近一次更新时间为 2019 年 12 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「parser」 「url」 「html」 「extract」 「Linkify」 「clickable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vstelmakh/url-highlight 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vstelmakh/url-highlight 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vstelmakh/url-highlight 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
Easy URL rewrites in your Laravel application
An MT940 bank statement parser for PHP
HTML and form generation
A Laravel helper to detect if the current route/path is active.
统计信息
- 总下载量: 962.68k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 104
- 点击次数: 31
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-12-22