将文本链接转换为锚点
strip_tags()为了防止用户放入奇数HTML并弄乱您编码良好的HTML,应该在用户生成的任何输入上使用该函数是常见的做法。您可能希望允许使用锚标记,这很好,但是某些用户可能会使用它来包含onmouseover和其他事件,以便在页面上运行JavaScript烦恼。
因此,从文本中删除所有标签后,您可以运行以下功能,将所有链接转换为锚元素。这将与您要在其上使用的任何纯文本一起使用。
function createTextLinks($str = '') {
if($str=='' or !preg_match('/(http|www\.|@)/im', $str)){
return $str;
}
//替换链接:
$str = preg_replace("/([ \t]|^)www\./im", "\\1http://www.", $str);
$str = preg_replace("/([ \t]|^)ftp\./im", "\\1ftp://ftp.", $str);
$str = preg_replace("/(https?:\/\/[^ )\r\n!]+)/im", "\\1", $str);
$str = preg_replace("/(ftp:\/\/[^ )\r\n!]+)/im", "\\1", $str);
$str = preg_replace("/([-a-z0-9_]+(\.[_a-z0-9-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)+))/im", "\\1", $str);
$str = preg_replace("/(\&)/im","\\1amp;", $str);
return $str;
}
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短