Roseline Mozano Roseline Mozano - 4 years ago 115
PHP Question

How to convert vimeo url to embed without letting go of the text around it

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&amp;byline=0&amp;portrait=0&amp;badge=0&amp;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 ?

Answer Source

Your function is ok but the way you call it is the problem

Replace

$message = convertVimeo($message);

With

$message = preg_replace_callback('#https://vimeo.com/\d*#', function($message) {  return convertVimeo($message[0]);
}, $message);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download