-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl_detect.php
39 lines (31 loc) · 1.29 KB
/
url_detect.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
<?php
#######################################################################
## URL DOESN'T REQUIRE http:// OR https://
## WILL DETECT IF LINK FORMAT IS CORRECT AND THEN ADD http:// BY ITSELF
#######################################################################
//The text to look for the link
$text = "The text you want to filter goes here. www.google.com and www.facebook.com";
//Call the function
echo turnUrlIntoHyperlink($text);
function turnUrlIntoHyperlink($string){
//The Regular Expression filter
$reg_exUrl = "/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/";
// Check if there is a url in the text
if(preg_match_all($reg_exUrl, $string, $url)) {
// Loop through all matches
foreach($url[0] as $newLinks){
if(strstr( $newLinks, ":" ) === false){
$link = 'http://'.$newLinks;
}else{
$link = $newLinks;
}
// Create Search and Replace strings
$search = $newLinks;
$replace = '<a href="'.$link.'" title="'.$newLinks.'" target="_blank">'.$link.'</a>';
$string = str_replace($search, $replace, $string);
}
}
//Return result
return $string;
}
?>