【发布时间】:2016-05-11 07:12:09
【问题描述】:
更新已解决(在此处添加答案,以免浪费您的时间)这只是为了帮助同一泡菜中的其他人
我刚刚注意到链接文本在帖子中被剪切,将 html 部分变为:
<a href="https://twitter.com/digitalOcean/status/723205273226190848" target="_blank">https://twitter.com/digitalOcean/sta...05273226190848</a>
因此失去了我替换的/status部分
把我的表情改成
~(<a href="https?://twitter.com)/(.*)/status/(.*)">(https?://twitter.com)/(.*)<\/a>~
解决了问题,对不起那些家伙=)
帮助他人的原创问题
我使用 preg_replace 发布了另一个关于我的插件的问题,但没有得到回复,所以我开始学习更多(总是一个好主意);)
我设法想出了一个更好的解决方案,但我需要一点帮助。这是我的新代码:
// embed tweet https://twitter.com/DrCustUmz/status/726561400617840641
$embed_tweet = '
<blockquote class="twitter-tweet" data-cards="hidden" data-lang="en">
<a href="https://twitter.com/\2/status/\3"></a>
</blockquote>
';
$this->post['message'] = preg_replace('~(<a href="https?://twitter.com)/(.*)/status/(.*)">(https?://twitter.com)/(.*)/status/(.*)<\/a>~', $embed_tweet, $this->post['message']);
// embed vine http://vine.co/v/bXZre5gtYgT/embed/postcard
$embed_player = '<iframe src="https://vine.co/v/\\4" width="480" height="480" frameborder="0"></iframe>';
$this->post['message'] = preg_replace('~(<a href="https?://vine.co)/v/(.*)">(https?://vine.co)/v/(.*)<\/a>~', $embed_player, $this->post['message']);
那它有什么作用?
好问题,在我的网站上,当用户发布链接时,一旦提交了帖子,它就会将其格式化为 href。 IE。发帖http://vine.co/v/bXZre5gtYgT/embed/postcard 变成<a href="http://vine.co/v/bXZre5gtYgT/embed/postcard" target="_blank">http://vine.co/v/bXZre5gtYgT/embed/postcard</a>
此替换的作用是查找 twitter 和 vine 链接并自动插入该帖子的嵌入代码。
那怎么了?
Vine 替换我还没有发现任何错误,但 Twitter 部分不想嵌入以下链接:
https://twitter.com/digitalOcean/status/723205273226190848
但确实替换:
https://twitter.com/DrCustUmz/status/726561400617840641
and
https://twitter.com/vBulletin/status/697116051079692288
我这辈子都想不通,因为除了用户名和帖子编号之外,链接没有任何区别。那我的表情可能有问题?
这里又是要替换的链接:
https://twitter.com/digitalOcean/status/723205273226190848
这是我的正则表达式:
~(<a href="https?://twitter.com)/(.*)/status/(.*)">(https?://twitter.com)/(.*)/status/(.*)<\/a>~
我没有看到一个问题...
是的...关于那个,我往往会忘记特别提出一个问题,我想你们知道我想弄清楚的是什么,哈哈
所以我猜...为什么我的preg_replace 对某些链接有效,但不是全部?
对此的任何帮助将不胜感激:)
这不是我之前的问题的重复,我已经自己解决了几乎所有问题,并且几乎准备好自己回答。
【问题讨论】:
-
提示!输入您的解决方案作为答案,其他人会发现它更容易。答案被接受的问题比没有答案的问题更有可能得到关注。
标签: php regex twitter hyperlink preg-replace