preg_replace twitter url不能用问号php进行操作

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

我已经创建了下一个函数,用一个 div 与它的id。

 function twitterIzer($string){
  $pattern = '~https?://twitter\.com/.*?/status/(\d+)~';

   $string = preg_replace($pattern, "<div class='tweet' id='tweet$1' tweetid='$1'></div>", $string);    

return $string;
  }

当我使用这种类型的url时,它工作得很好。

 https://twitter.com/Minsa_Peru/status/1260658846143401984

但它检索到的是一个优秀的 ?s=20 当我使用这个网址时

https://twitter.com/Minsa_Peru/status/1262730246668922885?s=20

我如何才能删除这个 ?s=20 文本,以使我的功能工作?任何我知道的是,我需要改进我的regex模式。谢谢你。

php preg-replace
1个回答
0
投票

如果你只想用regex。

$pattern = '/https?:\/\/twitter\.com\/.*?\/status\/(\d+)(.*)?/';

因为?不是一个数字,所以会用... (.*)这意味着所有的东西都在休息,在这种情况下是 ?s=xyz,最后一个问号 ? 是说,是可以存在或不存在。

学习regex

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