【问题标题】:preg_replace a hashtag that doesn't end with ;preg_replace 不以 ; 结尾的主题标签
【发布时间】:2016-12-07 18:28:04
【问题描述】:

我目前正在使用preg_replace 将提到的主题标签替换为 html 链接,如下所示。问题是有可能会检查 html 代码。所以一些css比如color: #000000;会强制它尝试将那个十六进制代码转换成一个链接。

如果单词的最后一个字母是;,我基本上需要我的正则表达式来忽略任何 preg_replace。这是我目前拥有的:

$str = preg_replace('/#([a-zA-Z0-9!_%]+)/', '<a href="http://example.com/tags/$1">#$1</a>', $str);

示例输入:'I like #action movies!'
预期输出:I like &lt;a href="http://example.com/tags/action"&gt;#action&lt;/a&gt; movies!'

我不能使用字符串的结尾来检查这一点,因为在任何给定时间都会检查文本块,因此提供的字符串可能是 #computer text text text #computer。

感谢任何帮助。

【问题讨论】:

  • 你能在这里举一个例子来解释你的输入字符串是什么以及预期的输出是什么......?
  • 我用一个例子编辑了我的帖子。链接格式正确且符合预期,我只需要调整我的正则表达式,使其忽略带有 ; 的单词。最后。

标签: php regex


【解决方案1】:

在您的正则表达式中,您可以检查主题标签旁边是否有 ;、非字母数字、行尾或字符串结尾:

/#([a-zA-Z0-9!_%]+)([^;\w]{1}|$)/

然后相应地使用 $1 和 $2

'<a href="http://example.com/tags/$1">#$1</a>$2'

你的代码看起来像

$str = preg_replace('/#([a-zA-Z0-9!_%]+)([^;\w]{1}|$)/', '<a href="http://example.com/tags/$1">#$1</a>$2',$str);

这里你可以看到一些测试:https://regex101.com/r/yN4tJ6/65

【讨论】:

    【解决方案2】:

    直到一个正则表达式大师来拯救你(如果有的话......),因为你在 PHP;这是一个只有几行代码的解决方案。

    $str="hi #def; #abc #ghi"; // just a test case (first one need be skipped)
    
    if (preg_match_all('/#([a-zA-Z0-9!_%]+.?)/', $str,$m)){
       foreach($m[1] as $k) if(substr($k,-1)!=';') {
          $k=trim($k);
          $str=str_replace("#$k","<a href='http://wxample.com/tags/$k'>#$k</a>",$str);
        }
    }
    
    print "$str\n";
    

    【讨论】:

      【解决方案3】:

      你可以添加一个条件来检查最后一个字符串是 ;或不并相应地使用它。 示例:

      if (substr($str, -1)==';'){
      //do nothing 
      }
      else {
      $str = preg_replace('/#([a-zA-Z0-9!_%]+)/', '<a href="http://example.com/tags/$1">#$1</a>', $str);
      }
      

      希望对您有所帮助。

      【讨论】:

        【解决方案4】:

        这个正则表达式应该可以工作:

        #([\w!%]+(?=[\s,!?.\n]|$))
        

        演示:https://regex101.com/r/KrRiD3/2

        您的 PHP 代码:

        $str = 'I like #strategy games #f1f1f1; #e2e2e2; #action games!';
        $str = preg_replace('/#([\w!%]+(?=[\s,!?.\n]|$))/', '<a href="http://example.com/tags/$1">#$1</a>', $str);
        echo $str;
        

        输出:

        我喜欢#strategy游戏#f1f1f1; #e2e2e2; #action游戏!

        【讨论】:

        • 如果主题标签位于字符串末尾,则不匹配
        • @phobia82 谢谢你的收获 :) 我更新了我的代码。
        【解决方案5】:

        好吧,你可以使用下面的代码,其实我是正则表达式的新手,所以它不是那么专业,但它可以工作,这里是

        $data = "<p style='color:#00000;'>Heloo</p> #computer text text text #computer #say #goo1d #sd! #say_hello";
        echo preg_replace("/(?<!\:)(\s+)\#([\w]+)(?!\;)/",'<a href="http://example.com/tags/$2">#$2</a>',$data);
        

        这个表达我用过

        /(?<!\:)(\s+)\#([\w]+)(?!\;)/
        

        输出是

        <p style='color:#00000;'>Heloo</p> <a href="http://example.com/tags/computer">#computer</a> text text text <a href="http://example.com/tags/computer">#computer</a> <a href="http://example.com/tags/say">#say</a> <a href="http://example.com/tags/goo1d">#goo1d</a> <a href="http://example.com/tags/sd">#sd</a>! <a href="http://example.com/tags/say_hello">#say_hello</a>
        

        我希望它对某人有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-04-18
          • 2020-04-18
          • 1970-01-01
          • 1970-01-01
          • 2016-05-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多