繁体   English   中英

PHP查找和Preg_replace字符串中的正则表达式

[英]PHP Find and Preg_replace a regex in a string

我希望正则表达式只用tweet id替换。 我试过下面的正则表达式,但不起作用。

$string = "The meeting, which was also attended by Chief Bisi Akande, held at the Abuja House in London, a tweet from the presidency revealed.

[twt]https://twitter.com/NGRPresident/status/829704039952740352[/twt]

[twt]https://mobile.twitter.com/NGRPresident/status/829713387001114625[/twt]

President Buhari has been away from Nigeria since January when he departed for a 10-day medical vacation.";
$regex = '/\[twt\]\/https?://twitter\.com/(?:\#!/)?(\w+)/status(es)?/(\d+)[\/twt\]/';
$string = preg_replace("$regex", '$3', $string);
echo $string;'

我想要结果输出;

这次会议由总统府发布的推文在伦敦阿布贾众议院举行。

829704039952740352

829713387001114625

布哈里总统自1月份离开尼日利亚后,离开了为期10天的医疗假期。

尝试这个:

/\[twt\]https?:\/\/(?:\w+\.)?twitter\.com\/\w+\/status(?:es)?\/(\d+)\[\/twt\]/

替换为

$1

演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM