3 回答
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>
TA贡献1874条经验 获得超12个赞
可能有很多模式,取决于你的大脑是如何工作的。我只想寻找推特部分,然后在以下位置获取数字/status/
:
$result = preg_replace('~https?://twitter\.com/.*?/status/(\d+).*~', "<div id='$1'></div>", $string);
TA贡献1775条经验 获得超11个赞
为什么这么复杂?
$pattern = '#https?:\/\/|www\d{0,3}[.]|twitter\.com\/([^\/]+)\/status\/(.+)#';
$1 将是用户名,$2 是状态 ID。
- 3 回答
- 0 关注
- 121 浏览
添加回答
举报