只是一个关于正则表达式的简单问题:这段代码适用于我需要做的任何修饰吗? (即,这可以输入数据库并且安全吗?)
function markdown2html($text) {
$text = htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
// Strong Emphasis
$text = preg_replace('/__(.+?)__/s', '<strong>$1</strong>', $text);
$text = preg_replace('/\*\*(.+?)\*\*/s', '<strong>$1</strong>', $text);
// Underline
$text = preg_replace('/_([^_]+)_/', '<p style="text-decoration: underline;">$1</p>', $text);
//Italic
$text = preg_replace('/\*([^\*]+)\*/', '<em>$1</em>', $text);
// Windows to Unix
$text = str_replace('\r\n', '\n', $text);
// Macintosh to Unix
$text = str_replace('\r', '\n', $text);
//Paragraphs
$text = '<p>' . str_replace("\n\n", '</p><p>', $text) . '</p>';
$text = str_replace("\n", '<br />', $text);
// [Linked Text](Url)
$text = preg_replace('/\[([^\]]+)]\(([a-z0-9._~:\/?#@!$&\'()*+,;=%]+)\)/i', '<a href="$2">$1</a>', $text);
return $text;
}
最佳答案
不,绝对不是。
您的代码与 SQL 无关——它根本不修改 '
或 \
字符。将此函数的格式化功能与 SQL 转义混合在一起是愚蠢的。
在某些情况下,您的代码还可能引入 HTML 注入(inject)——我对链接正则表达式的 URL 特别怀疑。如果没有适当的解析器,我不会相信它。
关于php - 对于非常基本的 Markdown ,这些正则表达式可以工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14329483/