Luminova Framework

PHP Luminova: Convert Text to Links with Linkify

Last updated: 2026-11-28 20:57:12

Convert detected URLs, email addresses, phone numbers, mentions, hashtags, and map locations to HTML or Markdown links.

Use Linkify for plain text that should contain clickable references. Select the types expected in your content and pass stripTags: false when the input is already plain text. For HTML output, keep link attributes and custom destination prefixes under application control. Escape or sanitize untrusted content for its output context. Tag stripping alone does not provide HTML sanitization.


Usage

use Luminova\Utility\Linkify;

echo Linkify::format(
    'Email [email protected] or follow @amina',
    types: [Linkify::EMAILS, Linkify::MENTIONS],
    options: [Linkify::MENTIONS => ['base' => '/members/']],
    format: Linkify::FORMAT_MARKDOWN,
    stripTags: false
);
// Email [[email protected]](mailto:[email protected]) or follow [@amina](/members/amina)

Class Definition

  • Full namespace: Luminova\Utility\Linkify
  • This class is final.

Constants

ConstantPurpose
FORMAT_HTML, FORMAT_MARKDOWNSelect HTML or Markdown output.
MODE_LINK, MODE_STRIPConvert detected values into links or remove them.
URLS, EMAILS, MENTIONS, PHONES, HASHTAGS, MAPSSelect the kinds of text to detect.
OPTIONSLists all detection types in processing order.

Pass options directly to a single-type method. With format(), pass one set of options for every type or key separate sets by type, such as Linkify::URLS => ['base' => '/out?url='].

KeyTypePurpose
basestring\|nullPrefix for the encoded destination. Email and phone links use fixed schemes.
modestringChoose MODE_LINK or MODE_STRIP.
attributesarray<string,string>HTML attributes from trusted configuration. Empty strings render as boolean attributes.
filterarray{patterns?: string[], dropUnmatched?: bool}Literal, wildcard, or slash-delimited regular expression filters for detected values. In link mode, dropUnmatched removes detected values rejected by the filters. In strip mode, rejected values remain.

Methods

format

Normalize text and apply the selected detectors in the order defined by OPTIONS. When tag stripping is enabled, the underlying DOM-based normalizer can preserve its UTF-8 XML declaration in the output.

public static function format(
    string $text,
    array $types = [],
    array $options = [],
    string $format = self::FORMAT_HTML,
    bool $stripTags = true,
    bool $strictWhitespace = true
): string

Parameters:

ParameterTypeDescription
$textstringText to process.
$typesstring[]Link types to process. An empty list selects every supported type (default: []).
$optionsarray<string,mixed>Global or per-type link options (default: []).
$formatstringOutput format, either FORMAT_HTML or FORMAT_MARKDOWN (default: self::FORMAT_HTML).
$stripTagsboolWhether to strip HTML tags during initial normalization (default: true).
$strictWhitespaceboolWhether to request strict whitespace normalization (default: true).

Return:

string - Text after processing the selected matches.


formatAll

Process every supported link type and apply the same target to generated HTML anchors. For supported formats, this method normalizes the text and then delegates to format(), which applies its own default normalization. Use format() directly to control normalization in one pass.

public static function formatAll(
    string $text,
    string $format = self::FORMAT_HTML,
    string $target = '_self',
    bool $stripTags = true,
    bool $strictWhitespace = true
): string

Parameters:

ParameterTypeDescription
$textstringText to process.
$formatstringOutput format, either FORMAT_HTML or FORMAT_MARKDOWN (default: self::FORMAT_HTML).
$targetstringHTML link target. An empty string omits the attribute (default: '_self').
$stripTagsboolWhether to strip HTML tags during initial normalization (default: true).
$strictWhitespaceboolWhether to request strict whitespace normalization (default: true).

Return:

string - Text after processing the selected matches.


urls

Link detected HTTP, HTTPS, and www. references. URLs on the application host or its subdomains link directly. Other matches append the encoded value to the configured base, which defaults to APP_URL . '?redirect='.

public static function urls(
    string $text,
    string $format = self::FORMAT_HTML,
    array $options = []
): string

Parameters:

ParameterTypeDescription
$textstringText to process.
$formatstringOutput format, either FORMAT_HTML or FORMAT_MARKDOWN (default: self::FORMAT_HTML).
$optionsarray{base?: string\|null, mode?: string, attributes?: array<string,string>, filter?: array{patterns?: string[], dropUnmatched?: bool}}Options for this detector (default: []).

Return:

string - Text after processing the selected matches.

Examples:

Linkify::urls('Visit https://example.org', options: ['base' => '/out?url=']);

maps

Link map: references containing a place name or a comma-separated coordinate pair. The default destination is a Google Maps search.

public static function maps(
    string $text,
    string $format = self::FORMAT_HTML,
    array $options = []
): string

Parameters:

ParameterTypeDescription
$textstringText to process.
$formatstringOutput format, either FORMAT_HTML or FORMAT_MARKDOWN (default: self::FORMAT_HTML).
$optionsarray{base?: string\|null, mode?: string, attributes?: array<string,string>, filter?: array{patterns?: string[], dropUnmatched?: bool}}Options for this detector (default: []).

Return:

string - Text after processing the selected matches.

Examples:

Linkify::maps('map:3.139,101.687', Linkify::FORMAT_MARKDOWN);

emails

Link detected addresses that pass PHP email validation using the mailto: scheme.

public static function emails(
    string $text,
    string $format = self::FORMAT_HTML,
    array $options = []
): string

Parameters:

ParameterTypeDescription
$textstringText to process.
$formatstringOutput format, either FORMAT_HTML or FORMAT_MARKDOWN (default: self::FORMAT_HTML).
$optionsarray{base?: string\|null, mode?: string, attributes?: array<string,string>, filter?: array{patterns?: string[], dropUnmatched?: bool}}Options for this detector (default: []).

Return:

string - Text after processing the selected matches.

Examples:

Linkify::emails('Contact [email protected]');

mentions

Link usernames written with an @ prefix. The default destination prefix is ?user=. Filters match the username without the prefix.

public static function mentions(
    string $text,
    string $format = self::FORMAT_HTML,
    array $options = []
): string

Parameters:

ParameterTypeDescription
$textstringText to process.
$formatstringOutput format, either FORMAT_HTML or FORMAT_MARKDOWN (default: self::FORMAT_HTML).
$optionsarray{base?: string\|null, mode?: string, attributes?: array<string,string>, filter?: array{patterns?: string[], dropUnmatched?: bool}}Options for this detector (default: []).

Return:

string - Text after processing the selected matches.

Examples:

Linkify::mentions('Thanks @amina', options: ['base' => '/members/']);

phones

Link matching phone numbers using the tel: scheme. Spaces and hyphens are removed from the generated label and destination.

public static function phones(
    string $text,
    string $format = self::FORMAT_HTML,
    array $options = []
): string

Parameters:

ParameterTypeDescription
$textstringText to process.
$formatstringOutput format, either FORMAT_HTML or FORMAT_MARKDOWN (default: self::FORMAT_HTML).
$optionsarray{base?: string\|null, mode?: string, attributes?: array<string,string>, filter?: array{patterns?: string[], dropUnmatched?: bool}}Options for this detector (default: []).

Return:

string - Text after processing the selected matches.

Examples:

Linkify::phones('Call +60 123-456-789');

hashtags

Link hashtags using the default ?tag= destination prefix or a custom base. Filters match the tag without the #.

public static function hashtags(
    string $text,
    string $format = self::FORMAT_HTML,
    array $options = []
): string

Parameters:

ParameterTypeDescription
$textstringText to process.
$formatstringOutput format, either FORMAT_HTML or FORMAT_MARKDOWN (default: self::FORMAT_HTML).
$optionsarray{base?: string\|null, mode?: string, attributes?: array<string,string>, filter?: array{patterns?: string[], dropUnmatched?: bool}}Options for this detector (default: []).

Return:

string - Text after processing the selected matches.

Examples:

Linkify::hashtags('Explore #Luminova', options: ['base' => '/tags/']);

strip

Remove detected values for the selected types, then trim the result. For mentions and hashtags, the detector also removes the leading marker.

public static function strip(
    string $text,
    array $types = []
): string

Parameters:

ParameterTypeDescription
$textstringText to process.
$typesstring[]Link types to process. An empty list selects every supported type (default: []).

Return:

string - Remaining text.

Examples:

Linkify::strip('Contact [email protected]', [Linkify::EMAILS]); // Contact

stripAll

Apply strip() to every supported type.

public static function stripAll(string $text): string

Parameters:

ParameterTypeDescription
$textstringText to process.

Return:

string - Remaining text after all supported detectors run.