Skip to content

Instantly share code, notes, and snippets.

@ryasmi
Last active June 22, 2023 14:47
Show Gist options
  • Select an option

  • Save ryasmi/0fb9f6042c1e0af0d74f to your computer and use it in GitHub Desktop.

Select an option

Save ryasmi/0fb9f6042c1e0af0d74f to your computer and use it in GitHub Desktop.

Revisions

  1. ryasmi revised this gist Jan 23, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wrapURLs.php
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    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, $text) ? $url : 'http://'.$url;
    $href = preg_match($protocol_pattern, $url) ? $url : 'http://'.$url;
    return '<a href="'.$href.'" target="'.$target.'">'.$url.'</a>';
    }, $text);
    };
  2. ryasmi revised this gist Jan 22, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wrapURLs.php
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@

    return preg_replace_callback($url_pattern, function (array $url_array) use ($target) {
    $url = implode($url_array, '');
    $protocol_pattern = '/^(?:(?:https?|ftp):\/\/)/iu'
    $protocol_pattern = '/^(?:(?:https?|ftp):\/\/)/iu';
    $href = preg_match($protocol_pattern, $text) ? $url : 'http://'.$url;
    return '<a href="'.$href.'" target="'.$target.'">'.$url.'</a>';
    }, $text);
  3. ryasmi revised this gist Jan 22, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion wrapURLs.php
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,8 @@

    return preg_replace_callback($url_pattern, function (array $url_array) use ($target) {
    $url = implode($url_array, '');
    $href = preg_match('/^(?:(?:https?|ftp):\/\/)/iu', $text) ? $url : 'http://'.$url;
    $protocol_pattern = '/^(?:(?:https?|ftp):\/\/)/iu'
    $href = preg_match($protocol_pattern, $text) ? $url : 'http://'.$url;
    return '<a href="'.$href.'" target="'.$target.'">'.$url.'</a>';
    }, $text);
    };
  4. ryasmi revised this gist Jan 22, 2015. 2 changed files with 8 additions and 5 deletions.
    6 changes: 4 additions & 2 deletions wrapURLs.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,10 @@
    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 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) {
    return '<a href="' + url + '" target="' + target + '">' + url + '</a>';
    var protocol_pattern = /^(?:(?:https?|ftp):\/\/)/i;
    var href = protocol_pattern.test(url) ? url : 'http://' + url;
    return '<a href="' + href + '" target="' + target + '">' + url + '</a>';
    });
    };
    7 changes: 4 additions & 3 deletions wrapURLs.php
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,12 @@
    <?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';
    $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, '');
    return '<a href="'.$url.'" target="'.$target.'">'.$url.'</a>';
    $href = preg_match('/^(?:(?:https?|ftp):\/\/)/iu', $text) ? $url : 'http://'.$url;
    return '<a href="'.$href.'" target="'.$target.'">'.$url.'</a>';
    }, $text);
    };
  5. ryasmi revised this gist Jan 22, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion wrapURLs.php
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,6 @@

    $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) {
  6. ryasmi revised this gist Jan 22, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions wrapURLs.md
    Original file line number Diff line number Diff line change
    @@ -16,3 +16,6 @@ Returns a String in which URLs have been replaced with anchors tag linking to th
    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.
  7. ryasmi revised this gist Jan 22, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wrapURLs.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ 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`).

  8. ryasmi revised this gist Jan 22, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion wrapURLs.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    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.
    @@ -13,5 +14,5 @@ Returns a String in which URLs have been replaced with anchors tag linking to th
    ## Example
    ```js
    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">http://www.google.com</a>.'
    // Returns 'Google is pretty awesome! Take a look at <a href="http://www.google.com" target="_blank">http://www.google.com</a>.'
    ```
  9. ryasmi renamed this gist Jan 22, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  10. ryasmi created this gist Jan 22, 2015.
    17 changes: 17 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    # 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
    ```js
    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">http://www.google.com</a>.'
    ```
    8 changes: 8 additions & 0 deletions wrapURLs.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    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) {
    return '<a href="' + url + '" target="' + target + '">' + url + '</a>';
    });
    };
    12 changes: 12 additions & 0 deletions wrapURLs.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    <?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, '');
    return '<a href="'.$url.'" target="'.$target.'">'.$url.'</a>';
    }, $text);
    };