将文本链接转换为锚点
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; }