Generate youtube thumbnail from embed code using php - php

I have tried to strip the code below
<iframe width="560" height="315" src="//www.youtube.com/embed/QUVdnzC19oU" frameborder="0" allowfullscreen></iframe>
its working but not stripping the links properly all i get is
http://img.youtube.com/vi/www.youtube.comembedQUVdnzC19oU/0.jpg
instead of
http://img.youtube.com/vi/QUVdnzC19oU/0.jpg
Please find the php code below.
if(preg_match_all('#<iframe\s[^>]*src=[\"|\']([^\"\'\>]+)[^>].*?</iframe>#siu', $item->introtext, $iframesrc) >0){
if(isset($iframesrc[1])){
$vid = str_replace(
array(
'http://youtu.be/',
'http://www.youtube.com/embed/',
'http://youtube.googleapis.com/v/'), '', $iframesrc[1][0]);
$vid = preg_replace('#\/.*?#i', '', $vid);
if(!(empty($vid))){
$result = '' .
'<div class="vimage">
<a class="video-link vlink" href="'.$item->link.'" title="">
<img src="http://img.youtube.com/vi/'.$vid.'/0.jpg" />
<span class="play-icon"> </span>
</a>
</div>';
$item->introtext = str_replace($iframesrc['0'], '', $item->introtext);
}
}
}
Thanks in advance.

Replace
array(
'http://youtu.be/',
'http://www.youtube.com/embed/',
'http://youtube.googleapis.com/v/'), '', $iframesrc[1][0]);
by
array(
'//youtu.be/',
'//www.youtube.com/embed/',
'//youtube.googleapis.com/v/',
'http:', 'https:'), '', $iframesrc[1][0]);
The problem is that you URL das not start with http:// as your code expects. My solution will work with //, http:// and https://.
Also the following line is not useful, as it will never have any effect.
$vid = preg_replace('#\/.*?#i', '', $vid);

<?php
$url = "www.youtube.com/embed/QUVdnzC19oU";
$urltokens = explode("/", $url);
$vid_id = $urltokens[2];
echo "http://img.youtube.com/vi/" . $vid_id . "/0.jpg";
?>
Considering you don't have http:// in the middle, this code will work.

Related

How can i get image using preg_match_all

i need some help for get images. Im using preg_match_all function.
Source: <img alt="test" title="test" src="/data/brands/test.png">
how can i get full image url ?
And this is my code for text. I need add image here.
<?
$link = 'link';
$marka = '#<div class="test">(.*?)</div>#si';
$getir = file_get_contents($link);
preg_match_all($marka,$getir,$test1);
$test = $test1[0];
echo $test[0]; ?>
Thanks.
This might help you
$str = '<img alt="test" title="test" src="/data/brands/test.png">';
$regex = '#src="(.+?)">#';
preg_match($regex,$str,$match);
echo $match[1];
//prints: /data/brands/test.png

php preg_replace relative to absolute in url css

I am trying to find a regex to convert relative url in css code 'url()'. So far this is what I have:
$domain = "http://example.com/";
$html = "url(1.css), url(' 1.css'), url( \"1.css\")";
$rep['/url(\s*)\((\s*)"(\s*)(?!https?:\/\/)(?!data:)(?!#)/i'] = 'url("'.$domain;
$rep["/url(\s*)\((\s*)'(\s*)(?!https?:\/\/)(?!data:)(?!#)/i"] = "url('".$domain;
$html = preg_replace(
array_keys($rep),
array_values($rep),
$html
);
echo $html;
Current Output:
url(1.css), url('http://example.com/1.css'), url("http://example.com/1.css")
Desired Output:
url(http://example.com/1.css), url('http://example.com/1.css'), url("http://example.com/1.css")
Are you expecting this? Hope it will work fine.
Try this code snippet here
Regex: /url[\s]*\([\s]*(?!'|\")(?!https?:\/\/)(?!data:)(?!#)
Here this regex will match url ( and after that there will be no " or '.
<?php
ini_set('display_errors', 1);
$domain = "http://example.com/";
$html = "url(1.css), url(' 1.css'), url( \"1.css\")";
$rep['/url[\s]*\([\s]*"[\s]*(?!https?:\/\/)(?!data:)(?!#)/i'] = 'url("'.$domain;
$rep["/url[\s]*\([\s]*'[\s]*(?!https?:\/\/)(?!data:)(?!#)/i"] = "url('".$domain;
$rep["/url[\s]*\([\s]*(?!'|\")(?!https?:\/\/)(?!data:)(?!#)/i"] = "url(".$domain;
$html = preg_replace(
array_keys($rep),
array_values($rep),
$html
);
echo $html;

wordpress php change oembed code for videos

As most people know, wordpress uses oembed to fetch the embed code for videos when adding a youtube video to a post. For example, pasting:
https://www.youtube.com/watch?v=pATcvr3zAhg
Would output this in the html:
<iframe width="420" height="315" src="https://www.youtube.com/embed/pATcvr3zAhg" frameborder="0" allowfullscreen></iframe>
I want to change this markup so it uses fancybox and an image, so my markup should look like this:
<a class="fancybox.iframe various" href="VIDEO SOURCE"><img src="IMAGE HERE"></a>
I was using this code which originally replaced the video with an image and when clicked, changed to the video - however, I want to modify this code now so that it outputs the markup above.
add_filter( 'oembed_dataparse', function($str, $data, $url) {
if ( ($yt = $data->provider_name == 'YouTube') || ($vm = $data->provider_name == 'Vimeo') )
{
if($yt) $html = str_replace('feature=oembed', 'feature=oembed&autoplay=1', $str);
else $html = str_replace('" width=', '?autoplay=1" width=', $str);
$html = htmlentities($html, ENT_QUOTES);
$img = $data->thumbnail_url;
$title = esc_attr($data->title);
return '<img src="'. $img . '" onclick="this.outerHTML=\'' . $html . '\'" title="' . $title . '">';
}
return $str;
}, 10, 3);

PHP preg_replace not replacing anything

Can someone tell me why when I run this code the preg_replace function seems like it does nothing?
<?php
$string = 'waka http://video.webmfiles.org/big-buck-bunny_trailer.webm waka';
$search = '#http\:\/\/.\.webm #';
$replace = '<video width="320" height="240" controls><source src="$1" type="video/webm"></video>';
$url = preg_replace($search,$replace,$string);
echo $url;
?>
Is my $search string wrong? If so, how can I fix it? It's suppose to replace strings starting in http:// and ending in .webm and surround them with the html code needed to play the .webm video.
Here's how I'd do this...
$string = 'waka http://video.webmfiles.org/big-buck-bunny_trailer.webm waka';
$search = '/(https?\:\/\/.+?\.webm)\h/';
$replace = '<video width="320" height="240" controls><source src="$1" type="video/webm"></video> ';
$url = preg_replace($search,$replace,$string);
echo $url;
Output:
waka <video width="320" height="240" controls><source src="http://video.webmfiles.org/big-buck-bunny_trailer.webm" type="video/webm"></video> waka
Regex101 demo: https://regex101.com/r/qR1xJ7/2
I ended up figuring it out. I'm now using:
$search = '#http:\/\/([^\']+)\.webm #';

php youtube and links regex are combining and in turn rendering useless

i have a posting feature on my site that can embed links and youtube videos. the problem is, that the two clash together, and the youtube iframe ends up being a 404 page on my site. my codes for the youtube videos and links are below, but im not sure how to stop them from combining and ruining it.
by combining, i mean this http://www.youtube.com/watch?v=VhqiT2nWCVU turns to
<iframe src="http://www.youtube.com/watch?v=VhqiT2nWCVU">
which then turns into
<iframe src="">
sorry if i am unclear in any way. my codes are below.
function youtube($string)
{
return preg_replace(
'#(http://(www.)?youtube.com)?/(v/|watch\?v\=)([-|~_0-9A-Za-z]+)&?.*?#i',
'<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/$4?rel=0" frameborder="0" allowfullscreen></iframe>',
$string
);
}
$posted = youtube($posted);
$rexProtocol = '(https?://)?';
$rexDomain = '((?:[-a-zA-Z0-9]{1,63}\.)+[-a-zA-Z0-9]{2,63}|(?:[0-9]{1,3}\.){3}[0-9]{1,3})';
$rexPort = '(:[0-9]{1,5})?';
$rexPath = '(/[!$-/0-9:;=#_\':;!a-zA-Z\x7f-\xff]*?)?';
$rexQuery = '(\?[!$-/0-9:;=#_\':;!a-zA-Z\x7f-\xff]+?)?';
$rexFragment = '(#[!$-/0-9:;=#_\':;!a-zA-Z\x7f-\xff]+?)?';
// Solution 1:
function callback($match)
{
// Prepend http:// if no protocol specified
$completeUrl = $match[1] ? $match[0] : "http://{$match[0]}";
return '<a href="' . $completeUrl . '">'
. $match[2] . $match[3] . $match[4] . '</a>';
}
$posted = preg_replace_callback("&\\b$rexProtocol$rexDomain$rexPort$rexPath$rexQuery$rexFragment(?=[?.!,;:\"]?(\s|$))&",
'callback', $posted);
A popular solution to this problem is to use placeholders. On your first pass you turn all YouTube links into a placeholder, for example:
{{youtube:VhqiT2nWCVU}}
After that you run your normal link converter. And at the end your run yet another regex to turn all your palceholders into youtube embeds.

Categories