php - 对于非常基本的 Markdown ,这些正则表达式可以工作吗?

标签 php mysql regex code-injection

只是一个关于正则表达式的简单问题:这段代码适用于我需要做的任何修饰吗? (即,这可以输入数据库并且安全吗?)

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/

相关文章:

php - 在 php 中使用 openssl 进行 AES-256-CBC 加密/解密十六进制字符串

php - openbase_dir 和子目录

php - 如何获取登录用户的用户名

python - Notepad++ 正则表达式每隔一个字符位置插入随机字母或数字

php - 如何检查 xml 远程 url 是否存在,如果不存在则使用缓存的本地文件

PHP mail() BCC - 仅显示 To : header 中的最终收件人地址

mysql - SQL查询无法处理引号

mysql - 使用具有多个连接的长 SQL 查询获取记录

Javascript 使用基于 DD/MM/YYYY 的正则表达式将文本从文件拆分为数组

php - PHP 中 skype 名称的正则表达式