Friday, 24th Nov 2017 | Text Only | Home Page

emailexp() & urlexp(): E-mail Address & URL Expanders

Download emailexp() File !

Last updated: 27/04/04

These functions parse e-mail addresses and URLs typed in normal text and then encloses them in appropriate anchor tags.E-mail address are completed with mailto: after href= and absolute URLs have target="_blank" to the anchor tag. If you have a content management system, online forum, message board or any Web application in which users are likely to type e-mail addresses or URLs, this function will greatly enhance the usability of user-entered data. It will parse any e-mail address or URL ending in a recognised top-level domain name, as long as they follow a space, new line, or opening parenthesis.

function emailexp($email) {
$pattern = "/(?<!mailto:)(\\s|^|\()(\\w+[\\w-\._]*\\w+)@(\\w+)([\\w-\._]+)*(\.)";
$pattern .= "(com|[a-z][a-z]|net|org|edu|gov|info|biz|int|mil|pro|name|aero|coop|museum)";
$pattern .= "(?!<\/a>)([^\\w]|$)(?!<\/a>|<\/A>|\")/";
$emailrep = "$1<a href=\"mailto:$2@$3$4$5$6\">$2@$3$4$5$6</a>$7";
$nemail = preg_replace($pattern, $emailrep, $email);
return $nemail;
}

function urlexp($text) {
$pattern = "/(\\s|^|\()(http:\/\/)*(\\w+)([\\w-\.]+)*(\.)";
$pattern .= "(com|[a-z][a-z]|net|org|edu|gov|info|biz|int|mil|pro|name|aero|coop|museum)";
$pattern .= "([\/?]\\w+[\\w\.\/?=-_]*\\w+)*(?!<\/a>|<\/A>|\")([^\\w]|$)(?!<\/a>|<\/A>|\")/";
$hrep = "$1<a href=\"http://$3$4$5$6$7\" target=\"_blank\">$3$4$5$6$7</a>$8";
$nt = preg_replace($pattern, $hrep, $text);
return $nt;
}


Assuming the function is saved in a file called emailexp.php and the script is included as thus: include('emailexp.php');, use the following syntax to execute the functions!

Always wrap emailexp in urlexp so the former script is run first (some surnames or usernames may be confused with top level domain names)

e-mails and URLs:echo urlexp(emailexp($text));
e-mails alone:echo emailexp($text);
URLs alone:echo urlexp($text);

Test the Script!

Type some text with an e-mail address and/or Web address!




Download Script!Demo

MultiWebQuiz 1.3 and 1.4 are now available for download


British American Dictionary
PHP quiz management system, free PHP scripts, web programming, server-side, educational, test management, test manager, quiz manager, script, download, web application, random quiz script


© Copyright Multi Web Vista 2004 — Web application developed by Neil Gardner
Keywords: PHP, free scripts, PHP code, download, Quiz Management System, Quiz System, Test Management System, Online Test, MultiWebTest, Multi Web Test, Multi Web Vista, Web Designer Test, Site Designer Test, CIW Site Designer Test, certification, CIW practice exam