Facebook Link Preview: PHP + jQuery




Updates Fev 2014:
0. Upgraded to version 1.0.0;
1. Code splitted into classes;
2. Multiple textareas: Now you can have more than one textare for Link Previewing. Just add $("id").linkPreview().
3. Support to insert and select actions for database connection (thanks for all requests);

Update Dec 2013 thanks to Manish Singh github @ manni-s:
1. Added support for Metacafe, Dailymotion, College Humor, Blip, FunnyOrDie

Great news! I've added the support for mysql. In the files, there is an example of mysql database that can be configured. Plus, there's a class named Database. Change the credentials there. And now is not stuck to only one textarea, you can add as many as you want, take a look at new new code. Download that below.


Options
wha
option default value possible values function
imageQuantity -1 any integer set the max amount of images to be brought
placeholder What's in your mind any string set the placeholder of textarea

Important: Make sure the library php5-curl is installed and enabled on the server whether local or at webspace.

All of us know about the big phenomenon that Facebook is. And since they have always to keep inovating, new super interesting features appear. And the liking of Facebook Link Preview is almost unanimous, it's activated when some url is inserted in the field where you update your status.

Ok, it's cool But, have you stop to think how that works?! I did. And I got too curious. So, I started looking how to build one of it in PHP + Javascript, using jQuery, and could work like Facebook's and Google+'s and it can checked out in the example below.

Choose a thumbnail
No thumbnail
25.media.tumblr.com
:D
www.youtube.com
© 2011 Teleprompt/Warner Bros. Records Inc. "Blood Pressure" by Mutemath from Odd Soul, available now. Written, directed, and filmed in less than 24 hours du...
lab.leocardz.com
All of us know about the big phenomenon that Facebook is. And since they have always to keep inovating, new super interesting features appear. And the liking of Facebook Link Preview is almost unanimous, it's activated when some url is inserted in the field where you update your status. Ok, it's coo...
Only a text!

As the most important part of the algorithm is in PHP, that is server-side, the capacity of servers that it will run can influence the speed of its execution. And we do not even have to comment about the giants' servers, since they are ones of the biggest technology companies in the world.

The algorithm keeps tracking what you are typing in the status field and through regular expressions identifies a url. Thereafter, the text is in the field is passed to PHP that does all the work to analyze all the source code of the url found. If you enter more than one url, it will consider that the first one is the more relevant and it will create a preview.

Once the source code of the url is obtained, regular expressions begin to seek out and capture relevant informations on it. These informations is basically the title page, the images contained therein, and a brief description of the content covered in the page.

From the collection of information is assembled the page preview. I didn't speak more about the details of the code, giving examples of implementation, because the Link Preview is intrinsic to insert a url and generate the preview, but for developers who want to go further exploring the code, I'm providing the download of files with only one request: keep my reference in the codes, right?! I hope you enjoy!

This code is also available on my GitHub profile.

These scripts are 100% free, but would you like to buy me a coffee?!