输入电子邮件和网址即可点击 [英] Input of email and url to be clickable
问题描述
我有一个用PHP处理的表单.用户有时会将其电子邮件地址放入表格或URL中.这些通常会在我剥离标签输入后以文本形式出现.
I have a form that I process in PHP. Users sometimes put their email address in the form or URLs. These usually come out as text after I strip the input of tags.
最近,我的用户开始要求我在拉动显示输入内容的页面(现在是从数据库中拉出)时使其URL和电子邮件可点击.
Recently my users started asking me to make their URLs and emails clickable when they pull up a page that displays their input (now pulled from a db).
有人可以提出一个共同的模式或处理方式吗?基本上,如果有人以表格形式输入网址,那么在查看时如何使网址可点击而不是文本?
Could someone please suggest a common pattern or ways that this is handled? Basically, if someone enters a url in a form, how do I make the url clickable instead of text when viewed?
谢谢, 亚历克斯
推荐答案
您可以使用基于正则表达式的函数
You can use a regular expression based function like this
function autolink($message) {
//Convert all urls to links
$message = preg_replace('#([\s|^])(www)#i', '$1http://$2', $message);
$pattern = '#((http|https|ftp|telnet|news|gopher|file|wais):\/\/[^\s]+)#i';
$replacement = '<a href="$1" target="_blank">$1</a>';
$message = preg_replace($pattern, $replacement, $message);
/* Convert all E-mail matches to appropriate HTML links */
$pattern = '#([0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.';
$pattern .= '[a-wyz][a-z](fo|g|l|m|mes|o|op|pa|ro|seum|t|u|v|z)?)#i';
$replacement = '<a href="mailto:\\1">\\1</a>';
$message = preg_replace($pattern, $replacement, $message);
return $message;
这篇关于输入电子邮件和网址即可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!