php - preg 替换 twitter 状态和 http 或 https PHP

标签 php html twitter preg-replace status

我将从我拥有的开始:

// embed tweet
$embed_tweet =  '
  <blockquote class="twitter-tweet" data-cards="hidden" data-lang="en">
    <a href="https://twitter.com/\\4"></a>
  </blockquote>
';

$this->post['message'] = preg_replace('#(<a href="http://twitter.com)?/(status/([-|~_0-9A-Za-z]+)|v\/([-|~_0-9A-Za-z]+)&?.*?)">(http://twitter.com)?/(status/([-|~_0-9A-Za-z]+)|v\/([-|~_0-9A-Za-z]+)&?.*?)<\/a>#i', $embed_tweet, $this->post['message']);

// embed vine
$embed_player =  '<iframe src="https://vine.co/v/\\4/embed/simple" width="480" height="480" frameborder="0"></iframe>';

$this->post['message'] = preg_replace('#(<a href="http://vine.co)?/(embed/([-|~_0-9A-Za-z]+)|v\/([-|~_0-9A-Za-z]+)&?.*?)">(http://vine.co)?/(embed/([-|~_0-9A-Za-z]+)|v\/([-|~_0-9A-Za-z]+)&?.*?)<\/a>#i', $embed_player, $this->post['message']);

我正在努力做到这一点,当用户发布指向 Twitter 状态或 Vine 帖子的链接时,它会自动嵌入该帖子。

Vine 代码是给我的,我设法更新了它的一部分,只有当它的 http 或我添加了 s 时才能让它工作我只能做到无论哪种方式,我都尝试通过它来使 Twitter 正常运行。

但我不知道如何为 Twitter 嵌入找到合适的 preg_replace

Twitter状态链接如下:

https://twitter.com/DrCustUmz/status/726561400617840641

因此,如果用户发布了这样的链接并且提交了帖子,它会变成:

<a href="https://twitter.com/DrCustUmz/status/726561400617840641" taget="_blank">https://twitter.com/DrCustUmz/status/726561400617840641</a>

那么我该如何实现这一目标,以及如何获得我需要的 preg_replace 呢?

另外... \\4 到底在行中做了什么

$embed_player =  '<iframe src="https://vine.co/v/\\4/embed/simple"...

最佳答案

继续学习更多的旅程,这就是我想出的。

// embed tweet
$drc_embed_tweet =  '
  <blockquote align="center" 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)/(.*)<\/a>~', $drc_embed_tweet, $this->post['message']);

// embed vine

$drc_embed_vine =  '<iframe align="center" src="https://vine.co/v/\2" width="480" height="480" frameborder="0"></iframe>';

$this->post['message'] = preg_replace('~(<a href="https?://vine.co)/v/(.*)">(https?://vine.co)/v/(.*)<\/a>~', $drc_embed_vine, $this->post['message']);

至于数字 \4 根据我的理解,这是 reg 的一部分。前任。自第二个注册以来被替换。前任。 \2 是相同的东西,可以使用任何一个数字。

关于php - preg 替换 twitter 状态和 http 或 https PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37149145/

相关文章:

PHP 7.2.7 : Attempted to call an undefined method named "setEncryptionName" of class "ZipArchive"

html - 将内容加载到 div (jQuery)、更改 url 并从侧边栏导航进行控制

javascript - jQuery如何计算实际屏幕距离?

html - 正在显示的 CSS 子菜单谷歌地图脚本

css - 有 twitter bootstrap 模态比例吗?

python - twitter api 转发排除

php - 如何计算在 laravel 中使用 DB::raw() 的位置?

php - 借助 PHP 和 HTML 动态创建行和列

php - 如何将 PHPExcel 合并单元格中的文本居中

hadoop - 如何使用pig从hdfs加载twitter数据?