youtube embed code with PHP - php

I have created a blog in php. Users can write something and post it. If this includes a web address then a link automated is created using this :
<?php
//...code
$row['comment'] = preg_replace('#(https?://([-\w.]+[-\w])+(:\d+)?(/([\w-.~:/?#\[\]\#!$&\'()*+,;=%]*)?)?)#', '<font color="#69aa35">$1</font>', $row['comment']);
?>
Using this in posts, text posted successfuly and web address displayed in a link format inside text. Any idea how can I change this, so that if there is a link of youtube, then a youtube frame to be created. For example in facebook, when you post a youtube address, a youtube frame created and posted instead of a link.

I have improved my answer and tested this code:
<?php
// This is your comment string containing the youtube link
$string="Here is a link - https://www.youtube.com/watch?v=LJHFXenOPi4";
// This will remove all links from the input string
preg_match('/[a-zA-Z]+:\/\/[0-9a-zA-Z;.\/?:#=_#&%~,+$]+/', $string, $matches);
foreach($matches as $url){
// Parse each url within the comment data
$input = parse_url($url);
if ($input['host'] == 'youtube.com' || $input['host'] == 'www.youtube.com' ) {
// If it is a youtube link, then parse the get variables
parse_str(parse_url($url, PHP_URL_QUERY), $variables);
// Echo out the iframe with the relevant video ID
echo '<iframe width="560" height="315" src="//www.youtube.com/embed/'.$variables['v'].'" frameborder="0" allowfullscreen></iframe>';
}
}
?>
I hope this is what you were looking for, it has worked for me on a few tests

You know the solution don't you? :) If the snippet contains youtube.com URL, then using the same pattern matching you can replace it with a youtube embed tag :)
Basically it will be something like this in pseudo-code.
Check the comment pasted to see if youtube URL present (modify the same regex you are using for finding URL, just make it specific for youtube)
If yes, then replace it with:
<iframe type="text/html"
width="640"
height="385"
src="<youtube URL>"
frameborder="0">
</iframe>

Related

How to get url of video element of an iframe using php

Good afternoon friend,
I am trying to be able to take the video from the url of the iframe, from what I see I have to first click the video so that the video element is visible in html.
Isn't there a way to automate the process? Click automatically when entering the url and extract the url from the video
The url is: https://feurl.com/v/2625zf2pddy2ge
I'm supposed to get the following url from the video at the end.
https://fvs.io/redirector?token=aVVHRmZNVzZVdldkRXJUZXdrSWRQV2RxQ2RSSjdFNGphTVBVQTVBRTR4TlpFYXdMbzlXaktueW9ETW5ma2QvYjlOZG42Mzg2eGNWSDNjT3BHUC8wMmxyUTcrZyt4ZzRwV0s4UWVLcWQzZExzdUVBN1dIbUVmSVhrbnlIWENwWHhFR09LRVBHcXpLUmg4NFlCaW10SzBGeVU2VXVNL3FvMjpUMXRDKytHYng5S1RTTU1laG0vbFZRPT0
My code is:
<?php
$data = file_get_contents('https://feurl.com/v/2625zf2pddy2ge');
preg_match('/<video[^>]*src=[\'"]([^\'"]+)[\'"][^>]*>/i', $data, $matches);
$video = $matches[1];
echo $video;
?>
Both links dont work but assuming html looks something like this a.html:
<video width="320" height="240" src="https://fvs.io/redirector?token=aVVHRmZNVzZVdldkRXJUZXdrSWRQV2RxQ2RSSjdFNGphTVBVQTVBRTR4TlpFYXdMbzlXaktueW9ETW5ma2QvYjlOZG42Mzg2eGNWSDNjT3BHUC8wMmxyUTcrZyt4ZzRwV0s4UWVLcWQzZExzdUVBN1dIbUVmSVhrbnlIWENwWHhFR09LRVBHcXpLUmg4NFlCaW10SzBGeVU2VXVNL3FvMjpUMXRDKytHYng5S1RTTU1laG0vbFZRPT0"></video>
Try this:
<?php
$data = file_get_contents('a.html');
preg_match('/<video.*?src="(.*?)"/', $data, $matches);
var_dump($matches[1]);
Output:
string(279) "https://fvs.io/redirector?token=aVVHRmZNVzZVdldkRXJUZXdrSWRQV2RxQ2RSSjdFNGphTVBVQTVBRTR4TlpFYXdMbzlXaktueW9ETW5ma2QvYjlOZG42Mzg2eGNWSDNjT3BHUC8wMmxyUTcrZyt4ZzRwV0s4UWVLcWQzZExzdUVBN1dIbUVmSVhrbnlIWENwWHhFR09LRVBHcXpLUmg4NFlCaW10SzBGeVU2VXVNL3FvMjpUMXRDKytHYng5S1RTTU1laG0vbFZRPT0"
If it doesnt work dump your $data and make sure the video tag is even there. Sometimes this is loaded later via ajax, so it is not even available in initial response.

How to play youtube video from MySQL database

I have a form which a user can enter a youtube link along with their product. I want to be able to play that video (which it's link is stored in a MySQL database, the problem is that the <iframe> tag needs a youtube "Embed" link and not the URL, is there a way to convert the URL to the Embed link?
video_url = https://www.youtube.com/watch?v=1DIDWWKk8Bg
//code to connect to MySQL DB and get all rows
$videourl = $row["video_url"];
}
?>
<iframe width="420" height="345" src="<?php $videourl?>">
</iframe>
Regular YouTube link look like this
https://www.youtube.com/watch?v=1234
Embed YouTube link look like this
https://www.youtube.com/embed/1234
You need just to replace this watch?v= to embed/
$link = "https://www.youtube.com/watch?v=1234";
echo str_replace("watch?v=", "embed/",$link);
You have to use embed.
like this.
$videourl = explode("https://www.youtube.com/watch?v=", $row['videourl']);
<iframe class="iframeVideo" src="https://www.youtube.com/embed/' . $videourl[1] . '"></iframe>
$videourl has to look like this https://www.youtube.com/watch?v=3J6o7hcm8bE
I hope this works for you.
You cannot not use the exact url from the youtube video. The url used in iframes has the following format http://www.youtube.com/embed/unique_id_here. So you only have to save the unique_id for each video. It is only a part of the url. For example, from this url: https://www.youtube.com/watch?v=hdhjs88737&list=RDEMnLv8DM0l6F9uGd8jrENGgg&index=3, the unique id is the one in bold: hdhjs88737. So here is what I would do. Go to any video on youtube and click the share option, under the options select embed. Then copy the code. It will look like this:
<iframe width="560" height="315" src="https://www.youtube.com/embed/<unique_id_here>" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
Modify the parameters in the iframe to fit your preferences then only save the unique_id to your database. That should solve your problem. Check this API reference for a detailed guide.
Here is what I used, I'm not sure if this code should be there twice but it works so...
<?php
str_replace("watch?v=", "embed/",$videourl);
?>
<iframe width="520" height="345" src="<?php echo str_replace("watch?v=", "embed/",$videourl);?>">
</iframe>
Thank you for the help.

Showing a preview of a youtube video when setting the link in comment php

I'm trying to develop in my website a preview of youtube link like facebook.
For example: If i set in comment a link of youtube video like https://www.youtube.com/watch?v=7TF00hJI78Y, a preview of this video will be showed in comment.
How can i do this??
Thanks.
You have to replace the link or url with an Iframe
For you
https://www.youtube.com/watch?v=7TF00hJI78Y
you have to replace that text for -->
<iframe width="560" height="315" src="https://www.youtube.com/embed/7TF00hJI78Y" frameborder="0" allowfullscreen></iframe>
https://www.youtube.com/embed/7TF00hJI78Y?start=500&end=600
start en end video and with javascript you can turn off the music.
and then onclick you can remove the start en end from the embed and put the music on method doesn't work anymore.

Wordpress- <iframe> is incorrectly interpreting url page content

I'm trying to embed a vimeo player in my Wordpress 3.8 site. To make changing the video a little easier, I've created Page which just contains the url for a Vimeo video. On the site, I try to embed the video like so:
<iframe allowfullscreen="" frameborder="0" mozallowfullscreen="" src="<? echo apply_filters('the_content', get_page(925)->post_content); ?>" webkitallowfullscreen=""></iframe>
But when I view the site, it just loads an empty page template inside the frame! When I echo that php chunk outside the iframe, however, it returns the url as expected. Putting in the url directly causes it to work fine, and I've tried a ton of quotation configurations to make sure the error wasn't there. Any idea what is causing it to wig out?
why are you running apply_filters on this? Also, get_page is deprecated.
Try something like this:
<?php
$vidPost = get_post(925);
echo '<iframe allowfullscreen="" frameborder="0" mozallowfullscreen="" src="'.$vidPost->post_content.'" webkitallowfullscreen=""></iframe>';
?>

Fetch user id from Soundcloud embed code

I need to embed soundcloud url in my website , SO I am asking site users to submit the SOUNDCLOUD URL they wish to embed on their page
Is there any way I can get using PHP
1. value of url in the iframe within src
2. and the user id i.e. 822654
<iframe width="100%" height="450" scrolling="no" frameborder="no" src="http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Fusers%2F847884 &show_artwork=true"></iframe>
I tried parse_url(PHP function) but failed to fetch what I want
If someone , can help me also know that whether every user gets an id like 847884 (in case above) or the embed code may have vanity url(like instead of 847884 can user have /bob/ or /andrew/) ?
Script:
<?php
$subject = "<iframe width=\"100%\" height=\"450\" scrolling=\"no\" frameborder=\"no\" src=\"http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Fusers%2F847884&show_artwork=true\"></iframe>";
$pattern = '/<iframe[^>]*\ssrc="[^"]*\?url=([^&]*%2Fusers%2F(\d+)[^&]*)&/';
preg_match($pattern, $subject, $matches);
echo urldecode($matches[1]), "\n";
echo $matches[2], "\n";
?>
Output:
http://api.soundcloud.com/users/847884
847884
See it and test it here.

Categories