TextLinkifier
in package
FinalYes
Turns web addresses written in text into clickable links (the paid "Show links" tariff of Discussion, Bazaar and Comcat).
Works on an HTML fragment that is already safe (sanitized editor HTML, or plain text escaped by
|breakLines): only text nodes outside an existing <a> change, and the DOM serializer escapes
them again, so no markup can enter through the address. Recognized are http://, https:// and
www. addresses; trailing punctuation of the sentence stays outside the link. Links get
rel="ugc nofollow noopener noreferrer" and open in a new tab, like Message::$externalUrl.
Table of Contents
Constants
- REL : mixed = 'ugc nofollow noopener noreferrer'
- URL_PATTERN : mixed = '~(?<![\w/@.])(?:https?://|www\.)[^\s<>"\']+~iu'
Methods
- linkify() : string
- href() : string|null
- linkifyTextNode() : void
- trimTrailingPunctuation() : string
- Sentence punctuation after an address is not part of it; `)` only when unbalanced.
Constants
REL
private
mixed
REL
= 'ugc nofollow noopener noreferrer'
URL_PATTERN
private
mixed
URL_PATTERN
= '~(?<![\w/@.])(?:https?://|www\.)[^\s<>"\']+~iu'
Methods
linkify()
public
static linkify(string $html) : string
Parameters
- $html : string
Return values
stringhref()
private
static href(string $url) : string|null
Parameters
- $url : string
Return values
string|nulllinkifyTextNode()
private
static linkifyTextNode(DOMDocument $document, DOMText $node) : void
Parameters
- $document : DOMDocument
- $node : DOMText
trimTrailingPunctuation()
Sentence punctuation after an address is not part of it; `)` only when unbalanced.
private
static trimTrailingPunctuation(string $url) : string
Parameters
- $url : string