Skip to content

Instantly share code, notes, and snippets.

@kennym
Forked from ryasmi/wrapURLs.js
Created December 21, 2016 22:43
Show Gist options
  • Save kennym/072694c2ab0e27a8eb65373d04865e49 to your computer and use it in GitHub Desktop.
Save kennym/072694c2ab0e27a8eb65373d04865e49 to your computer and use it in GitHub Desktop.
Wraps all URLs in anchor tags with a `href` and `target` inside some given text.
var wrapURLs = function (text, new_window) {
var url_pattern = /(?:(?:https?|ftp):\/\/)?(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}\-\x{ffff}0-9]+-?)*[a-z\x{00a1}\-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}\-\x{ffff}0-9]+-?)*[a-z\x{00a1}\-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}\-\x{ffff}]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?/ig;
var target = (new_window === true || new_window == null) ? '_blank' : '';
return text.replace(url_pattern, function (url) {
var protocol_pattern = /^(?:(?:https?|ftp):\/\/)/i;
var href = protocol_pattern.test(url) ? url : 'http://' + url;
return '<a href="' + href + '" target="' + target + '">' + url + '</a>';
});
};

wrapURLs

Wraps all URLs in anchor tags with a href and target inside some given text.

Parameters

Name Type Description
text String The text that may or may not containing URLs.
new_window Boolean Determines if the URLs should be opened in a new window (optional - defaults to true).

Returns

Returns a String in which URLs have been replaced with anchors tag linking to the URL.

Example

wrapURLs('Google is pretty awesome! Take a look at http://www.google.com.');
// Returns 'Google is pretty awesome! Take a look at <a href="http://www.google.com" target="_blank">http://www.google.com</a>.'

Credit

The URL Regex used here is taken from @diegoperini submission at https://mathiasbynens.be/demo/url-regex.

<?php
$wrapURLs = function ($text, $new_window = true) {
$url_pattern = '/(?:(?:https?|ftp):\/\/)?(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}\-\x{ffff}0-9]+-?)*[a-z\x{00a1}\-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}\-\x{ffff}0-9]+-?)*[a-z\x{00a1}\-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}\-\x{ffff}]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?/iu';
$target = $new_window ? '_blank' : '';
return preg_replace_callback($url_pattern, function (array $url_array) use ($target) {
$url = implode($url_array, '');
$protocol_pattern = '/^(?:(?:https?|ftp):\/\/)/iu';
$href = preg_match($protocol_pattern, $url) ? $url : 'http://'.$url;
return '<a href="'.$href.'" target="'.$target.'">'.$url.'</a>';
}, $text);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment