Viewed   54 times

I've been working on a little project, and I find myself in a position where I need a php function which can linkify URLs in my data, while enabling me to set some exceptions on links I don't want to linkify. Any idea of how to do this?



I have an open source project on GitHub: LinkifyURL which you may want to consider. It has a function: linkify() which plucks URLs from text and converts them to links. Note that this is not a trivial task to do correctly! (See: The Problem With URLs - ands be sure to read the thread of comments to grasp all the things that can go wrong.)

If you really need to NOT linkify specific domains (i.e. vimeo and youtube), here is a modified PHP function linkify_filtered (in the form of a working test script) that does what you need:

<?php // test.php 20110313_1200

function linkify_filtered($text) {
    $url_pattern = '/# Rev:20100913_0900
    # Match http & ftp URL that is not already linkified.
      # Alternative 1: URL delimited by (parentheses).
      (()                     # $1  "(" start delimiter.
      ((?:ht|f)tps?://[a-z0-9-._~!$&'()*+,;=:/?#[]@%]+)  # $2: URL.
      ())                     # $3: ")" end delimiter.
    | # Alternative 2: URL delimited by [square brackets].
      ([)                     # $4: "[" start delimiter.
      ((?:ht|f)tps?://[a-z0-9-._~!$&'()*+,;=:/?#[]@%]+)  # $5: URL.
      (])                     # $6: "]" end delimiter.
    | # Alternative 3: URL delimited by {curly braces}.
      ({)                     # $7: "{" start delimiter.
      ((?:ht|f)tps?://[a-z0-9-._~!$&'()*+,;=:/?#[]@%]+)  # $8: URL.
      (})                     # $9: "}" end delimiter.
    | # Alternative 4: URL delimited by <angle brackets>.
      (<|&(?:lt|#60|#x3c);)  # $10: "<" start delimiter (or HTML entity).
      ((?:ht|f)tps?://[a-z0-9-._~!$&'()*+,;=:/?#[]@%]+)  # $11: URL.
      (>|&(?:gt|#62|#x3e);)  # $12: ">" end delimiter (or HTML entity).
    | # Alternative 5: URL not delimited by (), [], {} or <>.
      (                        # $13: Prefix proving URL not already linked.
        (?: ^                  # Can be a beginning of line or string, or
        | [^=s'"]]          # a non-"=", non-quote, non-"]", followed by
        ) s*['"]?            # optional whitespace and optional quote;
      | [^=s]s+              # or... a non-equals sign followed by whitespace.
      )                        # End $13. Non-prelinkified-proof prefix.
      ( b                     # $14: Other non-delimited URL.
        (?:ht|f)tps?://      # Required literal http, https, ftp or ftps prefix.
        [a-z0-9-._~!$'()*+,;=:/?#[]@%]+ # All URI chars except "&" (normal*).
        (?:                    # Either on a "&" or at the end of URI.
          (?!                  # Allow a "&" char only if not start of an...
            &(?:gt|#0*62|#x0*3e);                  # HTML ">" entity, or
          | &(?:amp|apos|quot|#0*3[49]|#x0*2[27]); # a [&'"] entity if
            [.!&',:?;]?        # followed by optional punctuation then
            (?:[^a-z0-9-._~!$&'()*+,;=:/?#[]@%]|$)  # a non-URI char or EOS.
          ) &                  # If neg-assertion true, match "&" (special).
          [a-z0-9-._~!$'()*+,;=:/?#[]@%]* # More non-& URI chars (normal*).
        )*                     # Unroll-the-loop (special normal*)*.
        [a-z0-9-_~$()*+=/#[]@%]  # Last char can't be [.!&',;:?]
      )                        # End $14. Other non-delimited URL.
//    $url_replace = '$1$4$7$10$13<a href="$2$5$8$11$14">$2$5$8$11$14</a>$3$6$9$12';
//    return preg_replace($url_pattern, $url_replace, $text);
    $url_replace = '_linkify_filter_callback';
    return preg_replace_callback($url_pattern, $url_replace, $text);
function _linkify_filter_callback($m)
{ // Filter out youtube and vimeo domains.
    $pre  = $m[1].$m[4].$m[7].$m[10].$m[13];
    $url  = $m[2].$m[5].$m[8].$m[11].$m[14];
    $post = $m[3].$m[6].$m[9].$m[12];
    if (preg_match('/b(?:youtube|vimeo).comb/', $url)) {
        return $pre . $url . $post;
    } // else linkify...
    return $pre .'<a href="'. $url .'">' . $url .'</a>' .$post;

// Create some test data.
$data = 'Plain URLs (not delimited):
foo bar...
foo bar...
foo bar...
foo bar...
foo bar...
foo bar...
foo's_name.txt) bar... (with ' and (parentheses))
foo http://[2001:0db8:85a3:08d3:1319:8a2e:0370:7348] bar... ([IPv6 literal])
foo http://[2001:0db8:85a3:08d3:1319:8a2e:0370:7348]/file.txt bar... ([IPv6] with path)
foo bar...
foo bar...
foo bar...
foo bar...
foo bar...
foo bar...
foo's_name.txt) bar... (with ' and (parentheses))
foo bar...
foo bar...
foo bar...
foo bar...
foo bar...
foo bar...
foo's_name.txt) bar... (with ' and (parentheses))
// Verify it works...
echo(linkify_filtered($data) ."n");


This employs a callback function to do the filtering. Yes, the regex is complex (but so it the problem as it turns out!). You can see the interactive Javascript version of linkify() in action here: URL Linkification (HTTP/FTP).

Also, John Gruber has a pretty good regex to do linkification. See: An Improved Liberal, Accurate Regex Pattern for Matching URLs. However, his regex suffers catastrophic backtracking under certain circumstances. (I've written to him about this, but he has yet to respond.)

Hope this helps! :)

Friday, November 25, 2022

Here's what worked best for me when trying to script this (in case anyone else comes across this like I did):

$ pecl -d php_suffix=5.6 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.0 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.1 install <package>
$ pecl uninstall -r <package>

The -d php_suffix=<version> piece allows you to set config values at run time vs pre-setting them with pecl config-set. The uninstall -r bit does not actually uninstall it (from the docs):

vagrant@homestead:~$ pecl help uninstall
pecl uninstall [options] [channel/]<package> ...
Uninstalls one or more PEAR packages.  More than one package may be
specified at once.  Prefix with channel name to uninstall from a
channel not in your default channel (

  -r, --register-only
        do not remove files, only register the packages as not installed

The uninstall line is necessary otherwise installing it will remove any previously installed version, even if it was for a different PHP version (ex: Installing an extension for PHP 7.0 would remove the 5.6 version if the package was still registered as installed).

Monday, December 12, 2022

It turned out to be a simple solution!

However you won't be able to do the visited / not visited differentiation.

    TextView contactWeb1 = (TextView) findViewById(;
    noteView.setLinkTextColor(; //for example
    Linkify.addLinks(noteView, Linkify.ALL);

My attempts to catch visited states:



Instead of


In res/ create folder color and create colors.xml in res/color/


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="">
      android:state_window_focused="true" android:color="#00ff00">
      android:state_window_focused="true" android:color="#00ff00">
    <item android:color="#FF00ff"/>

I have tried my best to catch visited states. I tried all the states a selector can take.

I might have missed In case you found out, share (:

ALTERNATE SOLUTION (works only for html links)

Set the font Color programatically

Drawback (Be carefull for this point)

  • You will have to catch whether it was visited or not (this is doable)

    This means that you are not overriding the visited links functionality.


TextView contactWeb1 = (TextView) findViewById(;
String desc = "<font color="red"><a href=''>Visit my site</a></font>";
Wednesday, August 3, 2022

Never used any of those, but they look interesting..

Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..

Friday, November 11, 2022

Look like you need to put http:// at the beginning of that URL. Without the protocol specifier Android appears to assume "content://" is the intended url type.

Saturday, December 10, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :