preg_replace()的正则表达式将URL推入id标记不起作用

问题描述 投票:0回答:2

我有下一个字符串

 $string= "
https://twitter.com/AdamaUK_/status/1262730246668922885 
text
https://twitter.com/wheat_daddy/status/1262629949908885511
text
https://twitter.com/AdamaUK_/status/1262730246668922885
text
https://twitter.com/wheat_daddy/status/1262629949908885511

";

我想通过preg_replace()通过它>

     <div id='1262730246668922885'></div> text
      <div id='1262629949908885511'></div> text
       <div id='1262730246668922885'></div> text
        <div id='1262629949908885511'></div> text

到目前为止,我已经尝试过但没有成功

       $pattern = "/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|twitter+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/";

  function getTweet($string) {
    return preg_replace('/@(\w+)/', "<div id='$1'></div>", $string);
        }

如何从Twitter网址获取ID并成功地将其替换为div标签?谢谢。

我有下一个字符串$ string =“ https://twitter.com/AdamaUK_/status/1262730246668922885文本https://twitter.com/wheat_daddy/status/1262629949908885511文本https://twitter.com/AdamaUK_/status / ...

php preg-replace
2个回答
0
投票

可能有很多模式,我只需要查找twitter部分,然后在/status/之后获取数字:


0
投票

为什么这么复杂?

© www.soinside.com 2019 - 2024. All rights reserved.