-
Notifications
You must be signed in to change notification settings - Fork 0
/
autolink.php
56 lines (53 loc) · 1.63 KB
/
autolink.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
* Adapted from https://plugins.trac.wordpress.org/browser/sem-external-links/trunk/sem-autolink-uri.php
* which is MIT/GPL licenced.
* Author: Denis de Bernardy & Mike Koepke
* Author URI: https://www.semiologic.com
*/
class AutoLinkExtension {
public static function auto_link_text(string $string) {
$string = preg_replace_callback(
"/
((?<![\"']) # don't look inside quotes
(\b
( # protocol or www.
[a-z]{3,}:\/\/
|
www\.
)
(?: # domain
[a-zA-Z0-9_\-]+
(?:\.[a-zA-Z0-9_\-]+)*
|
localhost
)
(?: # port
\:[0-9]+
)?
(?: # path
\/[a-z0-9:%_|~.-]*
(?:\/[a-z0-9:%_|~.-]*)*
)?
(?: # attributes
\?[a-z0-9:%_|~.=&#;-]*
)?
(?: # anchor
\#[a-z0-9:%_|~.=&#;-]*
)?
)
(?![\"']))
/ix",
function ($match) {
$url = $match[0];
$href = $url;
if (false === strpos($href, 'http')) {
$href = 'http://' . $href;
}
return '<a href="' . $href . '" rel="noreferrer">' . $url . '</a>';
},
$string
);
return $string;
}
}