Reputation: 33
I am creating a Chat and i have a function that takes a vimeo url and convert it into an embeded video and it works. The problem is that, when i add any other text to the string, it does not work meanwhile i would like to keep the text around it and still convert the vimeo link into and embeded video
This is my code that convert any vimeo link into an embeded video
<?php
function convertVimeo($url)
{
########################################################
//extract the ID
if(preg_match(
'/\/\/(www\.)?vimeo.com\/(\d+)($|\/)/',
$url,
$matches
))
{
//Si l'url de vimeo est trouve
//the ID of the Vimeo URL: 71673549
$id = $matches[2];
//set a custom width and height
$width = '640';
$height = '360';
//echo the embed code and wrap it in a class
return '<div class="videowrapper well"><iframe src="http://player.vimeo.com/video/'.$id.'?title=0&byline=0&portrait=0&badge=0&color=ffffff" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></div>';
}
//Fin de si l'url de vimeo est trouve
########################################################
}
//store the URL into a variable
$message = 'https://vimeo.com/33881199';
$message = convertVimeo($message);
echo $message;
?>
The code above works perfectly but when i do
<?php
//store the URL into a variable
$message = 'Some text before https://vimeo.com/33881199 and text after ';
$message = convertVimeo($message);
echo $message;
?>
It does not work anymore
How to make it keep the text around the video and yet display the video ?
Upvotes: 1
Views: 6868
Reputation: 1663
A quick function for generating embed URL.
public function generateVideoEmbedUrl($url){
//This is a general function for generating an embed link of an FB/Vimeo/Youtube Video.
$finalUrl = '';
if(strpos($url, 'facebook.com/') !== false) {
//it is FB video
$finalUrl.='https://www.facebook.com/plugins/video.php?href='.rawurlencode($url).'&show_text=1&width=200';
}else if(strpos($url, 'vimeo.com/') !== false) {
//it is Vimeo video
$videoId = explode("vimeo.com/",$url)[1];
if(strpos($videoId, '&') !== false){
$videoId = explode("&",$videoId)[0];
}
$finalUrl.='https://player.vimeo.com/video/'.$videoId;
}else if(strpos($url, 'youtube.com/') !== false) {
//it is Youtube video
$videoId = explode("v=",$url)[1];
if(strpos($videoId, '&') !== false){
$videoId = explode("&",$videoId)[0];
}
$finalUrl.='https://www.youtube.com/embed/'.$videoId;
}else if(strpos($url, 'youtu.be/') !== false){
//it is Youtube video
$videoId = explode("youtu.be/",$url)[1];
if(strpos($videoId, '&') !== false){
$videoId = explode("&",$videoId)[0];
}
$finalUrl.='https://www.youtube.com/embed/'.$videoId;
}else{
//Enter valid video URL
}
return $finalUrl;
}
Upvotes: 1
Reputation: 21
my solution for youtube link and vimeo link, i'm feel ok
function convertLinkToEmbed($videoLink, $width, $height)
{
$embed = '';
if (preg_match('/https:\/\/(?:www.)?(youtube).com\/watch\\?v=(.*?)/', $videoLink))
$embed = preg_replace("/\s*[a-zA-Z\/\/:\.]*youtube.com\/watch\?v=([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i", "<iframe width=\"" . $width . "\" height=\"" . $height . "\" src=\"//www.youtube.com/embed/$1\" frameborder=\"0\" allowfullscreen></iframe>", $videoLink);
if (preg_match('/https:\/\/vimeo.com\/(\\d+)/', $videoLink, $regs))
$embed = '<iframe src="http://player.vimeo.com/video/' . $regs[1] . '?title=0&byline=0&portrait=0&badge=0&color=ffffff" width="' . $width . '" height="' . $height . '" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
return $embed;
}
Upvotes: 2
Reputation: 2643
I wrote a function of a combination of answers here to make it very simpleto use. Only works for plain vimeo links but if this floats your boat:
public function linkURLs( $text )
{
$text = preg_replace('#https?://(www\.)?vimeo\.com/(\d+)#',
'<iframe class="videoFrame" src="//player.vimeo.com/video/$2" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>',
$text);
return $text;
}
Upvotes: 0
Reputation: 929
The answer is : Use preg_replace_callback()
Try this :
$doConvert = function($url) {
return convertVimeo($url[0]);
};
$message = preg_replace_callback('#https://vimeo.com/\d*#', $doConvert, $message);
echo $message;
The script will replace and apply you function convertVimeo for each pattern url vimeo.
Upvotes: 3