I couldn't find any url2html-solution here that met all
these three criteria:
1) Make links of standard web-urls (http://some.com), urls without
http (some.com) and emails
2) Urls from all possible protocols (not just http and ftp)
3) To not link delimiter characters surrounding a link, e.g.
"My site (www.site.com) is good!"
The following code takes care of all this. It is easy to
below add extra characters that you don't want in the
beginning/end of a link, and to add extra abbreviations that
should not be linked (below adopted for some common english
ones). By disallowing digits in the end of an url, time
stamps like 04.57 won't be linked.
It seems to work fine with almost any imaginable url but
please feel free to make corrections.
<?php
function linkify($text) {
$strip_lchrs =
"[^s()[]{}.,;:?!]"; //Not these
chars in beginning
$strip_rchrs =
"[^s()[]{}.,;:?!d]"; //Not these
chars in end
$prot_pat = $strip_lchrs .
"[S]*://[S]*.[S]*" . $strip_rchrs;
//abc://de.fg
$email_pat = $strip_lchrs . "[S]* [S]*.[S]*" . $strip_rchrs; //abc de.fg
$general_pat = $strip_lchrs . "[S]*.[S]*" .
$strip_rchrs; //abc.de
$preg_pattern = "/(" . $prot_pat .
"|" . $email_pat . "|" . $general_pat .
")/ei";
$preg_replace = "check_preg('$1')";
return preg_replace($preg_pattern, $preg_replace,
$text);
}
function check_preg($subpat) {
//These abbr. should not be linked
$exc_string = "/e.g|i.e|et.c/i";
//If it says abc://de.fg
if (preg_match("/[S]*://[S]*.[S]*/i",
$subpat) == 1) {
return "<a href='" . $subpat . "'
target='_top'>" . $subpat . "</a>";
}
//If it says abc de.fg
else if (preg_match("/[S]* [S]*.[S]*/i",
$subpat) == 1) {
return "<a href='mailto:" . $subpat .
"'>" . $subpat . "</a>";
}
//If it says "e.g." don't link
else if (preg_match($exc_string, $subpat) == 1) {
return $subpat;
}
//If it says abc.de
else
return "<a href='http://" . $subpat . "'
target='_top'>" . $subpat . "</a>";
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 130.243.210.11
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- https://master
.php.net/note/edit/74582
Del: integrated -- h
ttps://master.php.net/note/delete/74582/integrated
Del: useless -- http
s://master.php.net/note/delete/74582/useless
Del: bad code -- htt
ps://master.php.net/note/delete/74582/bad+code
Del: spam -- https:/
/master.php.net/note/delete/74582/spam
Del: non-english --
https://master.php.net/note/delete/74582/non-english
Del: in docs -- http
s://master.php.net/note/delete/74582/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/74582
Reject -- https://mast
er.php.net/note/reject/74582
Search -- https://
master.php.net/manage/user-notes.php
--
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
|