我已經創建了一個插件,將鏈接變成鏈接中內容的Facebook嵌入版本。我的問題是,如果我禁用插件的評論部分,則評論鏈接將成爲嵌入帖子(如果插件的帖子部分仍處於活動狀態)。區分正則表達式中的兩個幾乎相同的鏈接
讓我們來看看,所以我們有3個環節:
Facebook發佈
<a href="https://www.facebook.com/zuck/posts/10102577175875681" target="_blank">ONE</a>
評論
<a href="https://www.facebook.com/zuck/posts/10102577175875681?comment_id=1193531464007751" target="_blank">Two</a>
,並以評論
<a href="https://www.facebook.com/zuck/posts/10102577175875681?comment_id=1193531464007751&reply_comment_id=10102577641662241" target="_blank">Three</a>
回覆所有三個林KS與
https://www.facebook.com/zuck/posts/10102577175875681
在下面的代碼,如果條件是我設置切換,和這個職位的消息是等於什麼用戶的帖子,所以在這個例子中這個帖子消息等於上述三個環節開始。
這是我爲轉換這些鏈接而創建的插件。
if ($this->registry->options['drcae_facebook_comment_onoff']) {
// swaps facebook comment links to embed code
$drc_embed_facebook_cmt = '<div class="fb-comment-embed" data-include-parent="true" data-width="560" data-href="https://www.facebook.com/$3/posts/$4comment_id=$5"></div>';
$this->post['message'] = preg_replace('~<a (.*)href="(.*)facebook.com/(.*)/posts/(.*)?comment_id=(.*)"(.*)<\/a>~', $drc_embed_facebook_cmt, $this->post['message']);
}
if ($this->registry->options['drcae_facebook_post_onoff']) {
// swaps facebook post links to embed code
$drc_embed_facebook_post = '<div class="fb-post" data-href="https://www.facebook.com/$3/posts/$4"></div>';
$this->post['message'] = preg_replace('~<a (.*)href="(.*)facebook.com/(.*)/posts/(.*)"(.*)<\/a>~', $drc_embed_facebook_post, $this->post['message']);
}
我確實有這個翻轉的其他方式(後爲第一),但是這引起了評論嵌入的帖子,我身邊這讓通過檢查第一個評論這可能不是這樣做的最佳方式。
所以你可能已經注意到了我的正則表達式,它不是最大的,但是這正是我能夠自己完成新的正則表達式的東西。
~<a (.*)href="(.*)facebook.com/(.*)/posts/(.*)"(.*)<\/a>~
我選擇做我的正則表達式這樣所以它沒有問題,如果一個鏈接被格式化像它仍然會嵌入如下:
<a target="blank" href="https://www.facebook.com/USERNAME/posts/1234567890" alt="facebook post">LINK</a>
但現在我的第二個猜測我的工作,經過搜索,並沒有提出任何事情,我想我會尋求一些幫助。
我該如何區分這些鏈接以便張貼帖子,不要干涉評論/評論回覆?
更新1,嵌入式崗位
現在我的插件看起來像這樣
$drc_embed_facebook_post = '<div class="fb-post" data-href="https://www.facebook.com/$2/posts/$3"></div>';
$this->post['message'] = preg_replace('~<a (.*?)facebook\.com/([^/]+)/[^/]+/([0-9]+)(?:[?][^0-9]+([0-9]+)(?:&(.+))?)?</a>~', $drc_embed_facebook_post, $this->post['message']);
正則表達式的具體
~<a (.*?)facebook\.com/([^/]+)/[^/]+/([0-9]+)(?:[?][^0-9]+([0-9]+)(?:&(.+))?)?</a>~
我已經離開了開始一個懶惰的東西嗎?我相信...不限制www。 https:// ect ...(facebook.com之前的任何內容)
這部分工作,直接抓取鏈接到這裏的帖子是一些例子。
https://www.facebook.com/RyanNewMe/posts/616837631826216?pnref=story
https://www.facebook.com/zuck/posts/10102833246942211?pnref=story
https://www.facebook.com/zuck/posts/10102830259184701?pnref=story
這些鏈接不嵌入帖子。但是,如果我從他們全部刪除?pnref=story
,只有以下鏈接不起作用。
https://www.facebook.com/RyanNewMe/posts/616837631826216
讓我在這裏,但怎麼堆的問題!大聲笑,我明白它的作用,但在世界上你是如何拿出正確的正則表達式的我已經嘗試過後期發表的gens,tuts和posts,我不能拿出一個正則表達式來挽救我的生活大聲笑,只是長大了喜歡使用任何'(。*)'大聲笑。但是如果我正確地說明了這一點,我可以把整個'〜~' and use... 'preg_replace('~~'' –
and something feels off, this bit 'facebook\.com/[^/]+/[^/]+/([0-9]+)' should contain $1 and $2 we need to change zuck and the post number –
@DrCustUmz The end of that should be '(?:&(.+))?)?〜'放在一起,而且開始時可能很懶,例如:' Laurel