【问题标题】:Detect hash tag and srip the # for clean url检测哈希标签并删除 # 以获得干净的 url
【发布时间】:2013-11-19 11:24:03
【问题描述】:

考虑字符串

$tring = "e.g. i want to #sleep."

我能够使用检查哈希标签

echo preg_replace('/(#\w+)/','<a href="tag.php?tag=\1">\1</a>',$tring']);

我想要做的是发送前面没有哈希的标签,即<a href="tag.php?tag=sleep">#sleep</a> instead of <a href="tag.php?tag=#sleep">#sleep</a>

【问题讨论】:

    标签: hashtag clean-urls


    【解决方案1】:

    您可以稍微修改您的正则表达式以使其正常工作。

    echo preg_replace('/(#)(\w+)/i', '<a href="tag.php?tag=\2">\1\2</a>', $tring);
    

    这会创建两个分组,而不是您拥有的那个。 URL 链接中只使用了第二组单词。两者都用于显示测试。

    这将如何工作的示例

    e.g. i want to #sleep.   =>  e.g. i want to <a href="tag.php?tag=sleep">#sleep</a>.
    this is a #cow           =>  this is a <a href="tag.php?tag=cow">#cow</a>
    the #duck is #sleeping   =>  the <a href="tag.php?tag=duck">#duck</a> is <a href="tag.php?tag=sleeping">#sleeping</a>
    #regex are @awesome #fun =>  <a href="tag.php?tag=regex">#regex</a> are @awesome <a href="tag.php?tag=fun">#fun</a>
    

    如你所见,这也处理了字符串中的多个标签。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-19
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      相关资源
      最近更新 更多