Finding and linkifying things that look like links.
It focuses on: a main use case, short and fast code, and code-sharing between backend and frontend.
This package works for multiple languages, right now php and js.
Use Composer. And use require to get the latest stable version:
composer require alsvanzelf/linkify:dev-master
The basics:
- Anything that starts with
http://
,https://
orwww.
- No support for ftp, email addresses, basic auth, and other edge cases.
- No advanced validation, for example real tld's.
- Adds
http://
for the href when it was not given in the text.
Things that work:
- Case insensitive
- Unicode
- Paths, query strings, hashes, and other allowed characters
- Urls starting/ending a line or the whole string
- Not including punctuation after the url
- Urls enclosed in brackets
- Urls with brackets inside the url
Things that -currently- do not work:
- Urls which contain one of: @ |
$linkifier = new alsvanzelf\linkify\linkify();
$linkified = $linkifier->linkify($plain);
var linkified = linkify(plain);
Note the input string should NOT contain html to prevent XSS. As afterwards one can not differentiate the added anchors from existing html.
Pull Requests or issues are welcome!
For example on adding more languages, or perfecting the cases catched.
Thanks to inspiration from: