【问题标题】:preg_replace not workingpreg_replace 不工作
【发布时间】:2012-04-03 03:12:57
【问题描述】:

我的网站有这个功能。

function autolink($content) {
   $pattern = "/>>[0-9]/i" ;
   $replacement = "<a href=\"#$0\">>>$0</a>";
   return preg_replace($pattern, $replacement, $content, -1);

这是为了将某些字符变成可点击的超链接。

例如,(在线程上)当用户输入“>>4”来表示另一个回复数字 4 时,该函数可能很有用。

但它不起作用。字符不会转换为超链接。它们只是保留为纯文本。不可点击。

有人能告诉我这个函数有什么问题吗?

【问题讨论】:

    标签: function preg-replace


    【解决方案1】:

    所以目标是转换:

    This is a reference to the >>4 reply
    

    ...进入:

    This is a reference to the <a href="#4">&gt;&gt;4</a> reply
    

    ...哪里“>”是“>”的 HTML UTF-8 等价物。 (请记住,您不想创建 HTML 问题)

    问题:(1)替换中的引号忘记转义了(2)由于要隔离数字,需要用括号创建子模式供以后参考。

    一旦你这样做了,你就会到达:

    function autolink($contents) {
        return preg_replace( "/>>([0-9])/i",
                             "<a href=\"#$1\">&gt;&gt;$1</a>",
                             $contents,
                             -1
                           );
    }
    

    祝你好运

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多