Get youtube/vimeo id with pregmatch - php

How can I get the video ID for youtube and vimeo videos embed code? A youtube video embed code looks like this:
<iframe width="560" height="315" src="http://www.youtube.com/embed/dbPmWbqAJPs" frameborder="0" allowfullscreen></iframe>
where the ID in this is dbPmWbqAJPs.
A vimeo embed code looks like this:
<iframe src="http://player.vimeo.com/video/62468031?color=00ff00" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
where the ID in this example is 62468031.
I don't want to replace those exact ids, but rather have a pregmatch/pregreplace that finds the id in any embed of vimeo and of youtube.

try this
vimeo
$string='<iframe src="http://player.vimeo.com/video/62468031?color=00ff00" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
preg_match_all( '~http://player.vimeo.com/video/(.*?)\?~si',$string,$M);
echo ($M[1][0]);
youtube :
$string='<iframe width="560" height="315" src="http://www.youtube.com/embed/dbPmWbqAJPs" frameborder="0" allowfullscreen></iframe>';
preg_match_all( '~http://www.youtube.com/embed/(.*?)\"~si',$string,$M);
echo ($M[1][0]);

Try below code for php, it works without pregmatch
i.e. :
http://vimeo.com/22222222
http://player.vimeo.com/video/22222222
http://vimeo.com/channels/staffpicks/22222222
http://vimeo.com/groups/groupsname/22222222
PHP Code :
$vimeoUrl = 'http://vimeo.com/channels/channelsName/22222222';
$fetchVimeoIdArr = explode('/', $vimeoUrl);
$idCounter = count($fetchVimeoIdArr) - 1;
$vimeoId = $fetchVimeoIdArr[$idCounter];

Related

Displaying youtube video from mysql database using php?

I want to display youtube video on a page, the following is my code in order to fetch youtube video URL from the database stored in mysql and then display it by iterating using while loop. However, the iframe does not display any video inside it.
<?php
if ($get_result != false) {
while ($row = mysqli_fetch_array($get_result, MYSQLI_ASSOC)) {
?>
<iframe width="560" height="315" src="<?php echo $row["url"]; ?>" frameborder="0" allowfullscreen></iframe>
<?php
}
}
?>
I have also tried using video tag inside while loop but it does not display the Youtube video inside the video player.
Please help me to solve this issue.
You can use Youtube with embed, like this:
<iframe width="560" height="315" src="https://www.youtube.com/embed/<?php echo $row["url"]; ?>?autoplay=1&autohide=1&controls=1&showinfo=0&modestbranding=1&rel=0"></iframe>
The problem is with your URL value.Replace "watch?v=" in video url with "v/" and try again.For example I tried with
<iframe width="560" height="315"
src="https://www.youtube.com//v/txSGdNONUJE" frameborder="0" allowfullscreen></iframe>
The above code will work fine.

Strip a section of an embed code with PHP

I'm trying to search JW Player embed codes like this one:
<iframe src="//content.jwplatform.com/players/ewQYJ6zA-F6KYzWLn.html" width="480" height="270" frameborder="0" scrolling="auto" allowfullscreen></iframe>
What I need to grab is the code right after players/ which in this embed would be just ewQYJ6zA.
Anyone have an idea how to go about this?
This will search for players/ followed by one or more letters and numbers.
<?php
$string = '<iframe src="//content.jwplatform.com/players/ewQYJ6zA-F6KYzWLn.html" width="480" height="270" frameborder="0" scrolling="auto" allowfullscreen></iframe>';
preg_match( '~/players/([[a-zA-Z\d]+)~', $string, $matches );
echo $matches[1];
?>
Output:
ewQYJ6zA
This regex in PHP should do the trick:
preg_match('/players\/(.+)-/im', $codetomatch, $codeyouwant);
Regex101

How to get all YouTube iframe from HTML using regex

I want to get all YouTube iframe using regex and want to add specific tag to each record found.
For example <youtube-frame></youtube-frame> to iframe begeing and end.
Needed output:
<youtube-frame><iframe width="560" height="315" src="https://www.youtube.com/embed/vakfMRyjulw" frameborder="0" allowfullscreen></iframe></youtube-frame>
<youtube-frame><iframe width="560" height="315" src="https://www.youtube.com/embed/aDGWMlKPKDs" frameborder="0" allowfullscreen></iframe></youtube-frame>
My Code
$embed = '
<iframe width="560" height="315" src="https://www.youtube.com/embed/vakfMRyjulw" frameborder="0" allowfullscreen></iframe>
<iframe width="600" height="350" src="https://tune.pk/player/embed_player.php?vid=6508414&folderp2016/05/04/&width=600&height=350&autoplay=no" frameborder="0" allowfullscreen scrolling="no"></iframe>
<iframe width="560" height="315" src="https://www.youtube.com/embed/aDGWMlKPKDs" frameborder="0" allowfullscreen></iframe>
<iframe width="600" height="350" src="https://tune.pk/player/embed_player.php?vid=6508414&folder=2016/05/04/&width=600&height=350&autoplay=no" frameborder="0" allowfullscreen scrolling="no"></iframe>
<iframe width="600" height="350" src="https://tune.pk/player/embed_player.php?vid=6508414&folder=2016/05/04/&width=600&height=350&autoplay=no" frameborder="0" allowfullscreen scrolling="no"></iframe>
';
What I have tried?
$pattern = '/<iframe\.*src=\"//youtube"\.*/';
$iframeSrc = preg_match($pattern, $embed, $matches);
var_dump($iframeSrc);
Try this:
$iframeSrc = preg_replace('/<iframe[^>]*src\s*=\s*"?https?:\/\/[^\s"\/]*youtube.com(?:\/[^\s"]*)?"?[^>]*>.*?<\/iframe>/i', '<youtube-frame>$0</youtube-frame>', $embed);
This uses preg_replace and a global regex to replace all YouTube IFrame tags (including their closing tags) with <youtube-frame>$0</youtube-frame> where $0 is the original string.
The regex could theoretically be simplified if you are totally sure about the format of your input, but I designed it to be robust enough to cope with other syntaxes such as src=http://example.com or src = "http://example.com" etc. which are accepted by browsers nowadays, and it only matches sources on *.youtube.com domains and not something like myyoutubesite.com.

Stripping out unwanted embed code

I am currently looking at a piece of code for a friend and I am trying to strip out a piece of unwanted code but not sure how I can achieve what I want.
CODE:
<?php
$blah = '<iframe src="//player.vimeo.com/video/82444237" width="500" height="281"
frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
<p>KING OF THE BEASTS from
John Wiley on
Vimeo.</p>';
echo($blah);
?>
In this code the website is showing: from on
I want the start point of the input to be iframe and the endpoint to be /iframe.
Without manually making sure the iframe only is selected, any suggestions on how to achieve this?
Try this:
function blah($postTag)
{
//TERMINATES THE STRING AT </IFRAME>
$exploder = explode("iframe",$postTag);
//CALLS THE STRING UNTIL THE FIRST IFRAME INPUT AND CLOSES THE IFRAME TAG
$cleaned = "<iframe".$exploder[1]."iframe>";
return $cleaned;
}
$blahblah = blah('<iframe src="//player.vimeo.com/video/82444237" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> <p>KING OF THE BEASTS from John Wiley on Vimeo.</p>');
That should do it.
A simple strstr() will do the job.
<?php
$blah = '<iframe src="//player.vimeo.com/video/82444237" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> <p>KING OF THE BEASTS from John Wiley on Vimeo.</p>';
echo strstr($blah,'</iframe>',true)."</iframe>";
OUTPUT
<iframe src="//player.vimeo.com/video/82444237" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
$blah = '<iframe src="//player.vimeo.com/video/82444237" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> <p>KING OF THE BEASTS from John Wiley on Vimeo.</p>';
echo substr($blah, 0, strpos($blah, '</iframe>')) ."</iframe>";
Output:
<iframe src="//player.vimeo.com/video/82444237" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>

Extract src from google maps embed link using regex

Hello everybody and thanks for reading.
I have a link like this :
<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.it/maps?f=q&source=s_q&hl=en&geocode=&q=monte+rosa&aq=&sll=45.454082,9.213138&sspn=0.009016,0.01929&t=h&gl=it&ie=UTF8&hq=&hnear=Monte+Rosa,+Province+of+Varese,+Lombardy&ll=45.690627,8.824349&spn=0.008978,0.01929&z=14&iwloc=A&output=embed"></iframe><br /><small>View Larger Map</small>
i want to extract only this
https://maps.google.it/maps?f=q&source=s_q&hl=en&geocode=&q=monte+rosa&aq=&sll=45.454082,9.213138&sspn=0.009016,0.01929&t=h&gl=it&ie=UTF8&hq=&hnear=Monte+Rosa,+Province+of+Varese,+Lombardy&ll=45.690627,8.824349&spn=0.008978,0.01929&z=14&iwloc=A&output=embed
In other words i want to extract everything after the src=" until the end of the link ".
I have been trying with the use of regex but i cant figure out the correct syntax. Some help would be most appreciated.
$html = '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.it/maps?f=q&source=s_q&hl=en&geocode=&q=monte+rosa&aq=&sll=45.454082,9.213138&sspn=0.009016,0.01929&t=h&gl=it&ie=UTF8&hq=&hnear=Monte+Rosa,+Province+of+Varese,+Lombardy&ll=45.690627,8.824349&spn=0.008978,0.01929&z=14&iwloc=A&output=embed"></iframe><br /><small>View Larger Map</small>';
preg_match('~iframe.*src="([^"]*)"~', $html, $result);
var_dump($result[1]);

Categories