为了账号安全,请及时绑定邮箱和手机立即绑定

preg_replace() 的正则表达式推文 url 到 id 标签不起作用

preg_replace() 的正则表达式推文 url 到 id 标签不起作用

PHP
摇曳的蔷薇 2023-04-02 11:05:22
我有下一个字符串 $string= "https://twitter.com/AdamaUK_/status/1262730246668922885 texthttps://twitter.com/wheat_daddy/status/1262629949908885511?s=20texthttps://twitter.com/AdamaUK_/status/1262730246668922885texthttps://twitter.com/wheat_daddy/status/1262629949908885511?s=30";我想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($pattern, "<div id='$1'></div>", $string);        }如何从推特网址获取 ID 并用标签成功替换它div?谢谢。
查看完整描述

3 回答

?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

不确定这是否是您要找的,但我认为


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


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

下面还根据要求删除了 ?s=20 。


这是我用来测试表达式的脚本:


<?php

$string= "

https://twitter.com/AdamaUK_/status/1262730246668922885

text

https://twitter.com/wheat_daddy/status/1262629949908885511?s=20

text

https://twitter.com/AdamaUK_/status/1262730246668922885

text

https://twitter.com/wheat_daddy/status/1262629949908885511?s=30


# Also caters for optional http, https and www:


https://www.twitter.com/AdamaUK_/status/1262730246668922885

text


http://www.twitter.com/AdamaUK_/status/1262730246668922885

text


http://twitter.com/AdamaUK_/status/1262730246668922885

text


www.twitter.com/AdamaUK_/status/1262730246668922885

text


twitter.com/AdamaUK_/status/1262730246668922885

text


";


$pattern = "~(?:https?:\/\/)?(?:www\.)?twitter\.com\/[^\/]+/status/(\d+)(?:.+)?~";

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



echo "Version where the text is inside the div (Just thinking <div id='1262730246668922885'>text</div> is better than <div id='1262730246668922885'></div> text)";


$pattern = "~(?:https?:\/\/)?(?:www\.)?twitter\.com\/[^\/]+/status/(\d+)(?:.+)?\R(.+)?~m";

echo preg_replace($pattern, "<div id='$1'>$2</div>", $string);


?>

在命令行执行时的结果:


$ php -f test.php 


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

text

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

text

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

text

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


# Also caters for optional http, https and www:


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

text


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

text


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

text


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

text


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

text


Version where the text is inside the div (Just thinking <div id='1262730246668922885'>text</div> is better than <div id='1262730246668922885'></div> text)

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

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

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

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

# Also caters for optional http, https and www:


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


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


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


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


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


查看完整回答
反对 回复 2023-04-02
?
HUWWW

TA贡献1874条经验 获得超12个赞

可能有很多模式,取决于你的大脑是如何工作的。我只想寻找推特部分,然后在以下位置获取数字/status/

$result = preg_replace('~https?://twitter\.com/.*?/status/(\d+).*~', "<div id='$1'></div>", $string);



查看完整回答
反对 回复 2023-04-02
?
繁星淼淼

TA贡献1775条经验 获得超11个赞

为什么这么复杂?

$pattern = '#https?:\/\/|www\d{0,3}[.]|twitter\.com\/([^\/]+)\/status\/(.+)#';

$1 将是用户名,$2 是状态 ID。


查看完整回答
反对 回复 2023-04-02
  • 3 回答
  • 0 关注
  • 121 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信