kwi-dk / UrlLinker

A PHP module for converting plain text to HTML, and any URLs in the text into HTML links.
9 stars 2 forks source link

UrlLinker

UrlLinker is a PHP module for converting plain text snippets to HTML, and any web addresses in the text into HTML hyperlinks.

Usage::

print(htmlEscapeAndLinkUrls($text));

For a longer example, see UrlLinker-example.php__.

__ https://bitbucket.org/kwi/urllinker/src/tip/UrlLinker-example.php

UrlLinker assumes plain text input, and returns HTML. If your input is already HTML, but it contains URLs that have not been marked up, UrlLinker can handle that as well::

print(linkUrlsInTrustedHtml($html));

Warning: The latter function must only be used on trusted input, as rendering HTML provided by a malicious user can lead to system compromise through cross-site scripting__. The htmlEscapeAndLinkUrls function, on the other hand, can safely be used on untrusted input. (You can remove existing tags from untrusted input via PHP's strip_tags__ function.)

__ http://en.wikipedia.org/wiki/Cross-site_scripting __ http://php.net/strip-tags

Note: This repository is no longer being actively maintained. If you want to use UrlLinker with Composer, consider this fork__.

__ https://github.com/MacDada/UrlLinker

Recognized addresses

__ http://en.wikipedia.org/wiki/Cross-site_scripting __ http://www.htmlhelp.com/tools/validator/problems.html#amp

Background

A Stackoverflow.com question__ prompted me to consider the difficulty of this task. Initially, it seemed easy, but like an itch you just have to scratch, I kept coming back to it, to fix just one more little thing.

__ http://stackoverflow.com/questions/1188129/replace-urls-in-text-with-html-links/

Feel free to upvote my answer if you find this code useful.

There's also a C# implementation__ by Antoine Sottiau.

__ http://codepaste.net/ngamud

Public Domain Dedication

To the extent possible under law, the author has waived all copyright and related or neighboring rights to UrlLinker.

For more information see: http://creativecommons.org/publicdomain/zero/1.0/