承接 vstelmakh/url-highlight 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vstelmakh/url-highlight

Composer 安装命令:

composer require vstelmakh/url-highlight

包简介

Library to parse urls from string input

README 文档

README

Url highlight logo

Build status Packagist version PHP version License

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

🚀 See examples 👀

Installation

Install the latest version with Composer:

composer require vstelmakh/url-highlight

Also, there are Twig logo Twig extension and Symfony logo 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&quot; or http://example.com?a=1&amp;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('&lt;a href=&quot;http://example.com&quot;&gt;Example&lt;/a&gt;');
// return: '&lt;a href=&quot;<a href="http://example.com">http://example.com</a>&quot;&gt;Example&lt;/a&gt;'

💡 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 vstelmakh/url-highlight 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 962.68k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 104
  • 点击次数: 31
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 102
  • Watchers: 5
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-12-22