PDA

View Full Version : Extract information from URL to use in bbcode.


Tjap
07-12-2013, 03:43 PM
Dear vBulletin users,

For a while, I've been using a twitter mod on my forum (3.8), it is a mod to embed a tweet by using the following syntax:

twitterstatusid

Now I would like to be able to just post the tweet url (which contains the statusid), now I figured the most simple solution would be to extract the statusid directly from the URL and add the bbcode. Now there is only one problem, I really don't know anything about PHP or XML.

Is there anyone who would like to help me out? It would be greatly appreciated.

Kind regards,
Nick

exel
07-13-2013, 08:09 AM
May I know what BBCODE mod you're using for this?

As I got a PHP code ready which I tested works perfectly and replaces for example:

https://twitter.com/exelaguilar/status/355224693311152129

To..

355224693311152129

But of course, I can't actually test the [tweet] bbcode as I don't have it.

Tjap
07-13-2013, 03:25 PM
I'm using the following mod:

https://vborg.vbsupport.ru/showthread.php?t=287342

It states that it is for vB4, but it works on vB3 as well

exel
07-13-2013, 05:56 PM
Make a plguin for postdata_presave and threadfpdata_presave

And add the following code to both:

global $vbulletin;
$pagetext =& $this->fetch_field('pagetext', 'post');

if (stristr($pagetext,'https://twitter.com/')) {


$string = $pagetext;
$pattern = '#https?://twitter\.com/(?:\#!/)?(\w+)/status(es)?/(\d+)#';
$replacement = '$3';

$pagetext = preg_replace($pattern, $replacement, $string);

}

Won't gurantee anything, but it does indeed convert a twitter link to the format you want.


See: http://elite-source.com/testthis.php

tbworld
07-13-2013, 07:07 PM
Interesting piece of code. Thanks @exel. :)

Tjap
07-14-2013, 02:42 PM
Thank you very much Exel, it works pretty well, except for the fact that vBulletin automatically adds [url] to the url, is there a way to fix this?

exel
07-14-2013, 10:15 PM
Here you go:


global $vbulletin;
$pagetext =& $this->fetch_field('pagetext', 'post');

if (stristr($pagetext,'https://twitter.com/')) {

function stripBBCode($text, $code)
{
$code = str_replace(",", "|", $code);
$code = str_replace(" ", "", $code);
$pattern = '#\[/?(?:' . $code . ')[^]]*\]#i';
return preg_replace($pattern, '', $text);
}

$string = $pagetext;
$pattern = '#https?://twitter\.com/(?:\#!/)?(\w+)/status(es)?/(\d+)#';
$replacement = '$3';

$pagetext = preg_replace($pattern, $replacement, $string);

$pagetext = stripBBCode($pagetext, "url");

}


Tested here; http://craftgraphics.com/forum/showthread.php?p=65#post65

Only issue with that is it will also strip out [url] for anything else. BUT it will ONLY do that if the post has a twitter link on it.

So if your post has

http://twitter.com/exel/status/123

and http://google.com

it will do

123

google.com


But if it doesn't have twitter in the post, it will use the default vbulletin system.

--------------- Added 1373865987 at 1373865987 ---------------

Can confirm it works:

https://vborg.vbsupport.ru/external/2013/07/36.png

Tjap
07-15-2013, 07:35 AM
Wow, thanks alot mate. Works perfectly!

exel
07-15-2013, 05:04 PM
Glad to hear. :)