Using embed.ly to only embed certain links
I'm using embed.ly to embed links when a user types in [embed=LINK]. I'm using the jQuery embed.ly plugin.
I only want links that are tagged as embed, to be embedded:
For example if there are two links:
http://www.youtube.com/watch?v=7aPGa9Gqj2c&feature=related
and
[embed=http://www.youtube.com/watch?v=7aPGa9Gqj2c&feature=related]
I only want the second link to be embedded, and the first one to appear as a link.
I use the following code to find all Embed tags:
if (preg_match_all('/\[Embed=([^\]]+)\]/i', $input['body'], $matches, PREG_SET_ORDER))
{
$get_embedly = true;
foreach ($matches as $match)
{
if (preg_match($embedly_re, $match[1])) // $embedly_re = list of sites allowed
{
$input['body'] = str_replace($match[0], '> '. "<a href=\"$match[1]\">$match[1]</a>", $input['body']);
}
}
}
I use jQuery and embedly in the header:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="js/embedly/jquery.embedly.js"></script>
Do you have any suggestions as to how I should go abou开发者_JS百科t this? Cheers.
You are 98% of the way there. There are just a couple things you should add/change and you will be set.
Change line 9 of your PHP code to add a class embedly
:
$input['body'] = str_replace($match[0], '> '. "<a class=\"embedly\" href=\"$match[1]\">$match[1]</a>", $input['body']);
Then use the class we just added to tell Embedly jQuery what <a>
tags to embed.
$('a.embedly').embedly(
{maxWidth: 500,
method: "replace"});
Done.
精彩评论