2014-01-27 35 views
0

我有一个textarea,我的用户需要被允许发布链接,邮件和图像。PHP允许img和锚 - 但排除其他标签

function autolink($message) 
{ 
    $text = " " . $message; 
    $text = preg_replace("#([\n ])([a-z]+?)://([a-z0-9\-\.,\?!%\*_\#:;~\\&[email protected]\/=\+]+)#i", "\\1<a href=\"\\2://\\3\" target=\"_blank\" class=\"link\">\\2://\\3</a>", $text); 
    $text = preg_replace("#([\n ])www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[a-z0-9\-\.,\?!%\*_\#:;~\\&[email protected]\/=\+]*)?)#i", "\\1<a href=\"http://www.\\2.\\3\\4\" target=\"_blank\" class=\"link\">www.\\2.\\3\\4</a>", $text); 
    $text = preg_replace("#([\n ])([a-z0-9\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)?[\w]+)#i", "\\1<a href=\"mailto:\\[email protected]\\3\" class=\"link\">\\[email protected]\\3</a>", $text); 

    $text = substr($text, 1); 
    return($text); 
} 

这个功能就像一个魅力,但如果我想添加一个替换img src的行 - 它不再工作。

$text = preg_replace('#<img.+?src="([^"]*)".*?/?>#i', '<a href="$1">$0</a>', $text); 

上述工作很好,但现在它不会取代锚和邮件作为链接。

我搜索了这个论坛,发现一个可能工作的答案,它包括php函数strip_tags();

$allowed = "<img><a>"; 
$formatted = strip_tags($_POST['usercomment'], $allowed); 

什么是最安全(和正确)的解决方案呢?我已经阅读了一些DOM,并且regulair表达式不适合这份工作,但我真的需要som来帮助解决这个问题。 如果没有与img标签一致的行,当用户发布链接(http://example.com)时,我的功能可以很好地工作,并且可以点击。

对不起,我的英语不好,描述的方式,这是我的第一篇帖子inhere。

+1

使用降价,期。 – moonwave99

+0

前3个正则表达式的工作如何?他们都被双引号错误地在字符串中转义。 – sln

+0

@sln如果用户写一个没有html的链接,当我输出内容时它会变成可点击的链接。我不知道它是如何工作的,对不起 – mhDK

回答

0

为什么不去掉所有东西,并让你的用户使用bbcode,我只是写了这个函数来做类似的事情。

function html2txt($document){ 
`$search = array('@<script[^>]*?>.*?</script>@si', // Strip out javascript 
      '@<[\/\!]*?[^<>]*?>@si',   // Strip out HTML tags 
      '@<style[^>]*?>.*?</style>@siU', // Strip style tags properly 
      '@<![\s\S]*?--[ \t\n\r]*>@',   // Strip multi-line comments including CDATA 
      '@[email protected]', 
      '@[email protected]', 
      '@[email protected]', 
      '@[email protected]', 
      '/<.*?>/' 
); 
$text = preg_replace($search, '', $document); 
return $text; 
} 

function display_bbcode($string){ 
    $search = 
    array(
    '~\[url(?|=[\'"]?+([^]"\']++)[\'"]?+]([^[]++)|](([^[]++)))\[/url]~', // URL 
    '/\[b\](.*?)\[\/b\]/ms', // Bold 
    '/\[i\](.*?)\[\/i\]/ms', // Italic 
    '/\[u\](.*?)\[\/u\]/ms', // Underline 
    '/\[img\](.*?)\[\/img\]/ms', //IMG 
    '/\[size\="?(.*?)"?\](.*?)\[\/size\]/ms', //Font Size 
    '/\[color\="?(.*?)"?\](.*?)\[\/color\]/ms', // Color 
    '/\[quote](.*?)\[\/quote\]/ms', //Quote 
    '/\[list\=(.*?)\](.*?)\[\/list\]/ms', //List 
    '/\[list\](.*?)\[\/list\]/ms', // Default List 
    '/\[\*\]\s?(.*?)\n/ms' 
); 
$replace = 
array(
'<a href="$1">$2</a>', // URL 
    '<strong>\1</strong>', // Bold 
    '<em>\1</em>', // Italic 
    '<u>\1</u>', // Underline 
    '<img src="\1" alt="\1" height=\'200px\' width=\'200px\'/>', //Image, Auto Re-size 
    '<font size="\1%">\2</font>',// Font Size 
    '<font color="\1">\2</font>', // Font Color 
    '<blockquote>\1</blockquote>', //Qoute 
    '<ol start="\1">\2</ol>', // Some List Style? 
    '<li>\1</li>',//Default List Style 
    '<li>\1</li>' 
    ); 
    $text = preg_replace($search, $replace, $string); 
    return $text; 
} 

然后,只需调用您的函数:

当用户发表内容: strip_tags(html2txt($input));

当显示内容: display_bbcode($output);

+0

那么用户输入他们自己的bbcode? – sln

+0

非常好的功能,但是因为我在textarea中通过imgur.com使用外部上传脚本,所以img标签被张贴在数据库中。如果我删除输出中的htmlspecialchars,它会发布图片,但其他html标签也将被允许。 – mhDK

+0

我使用NicEdit,脚本在数据库中发布html img标签。输出将会像'code' - 这样我的img替换行将它作为带imgur反向链接的图片发布 - 但是如果我删除了它正在工作的htmlspecialchars,但是其他html标记则被允许并且不太安全。 – mhDK

相关问题