preg_match and preg_replace for youtube url - php

i have this code
preg_match_all('%(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $asd, $match);
to find youtube key of urls like
http://www.youtube.com/watch?v=ZiJRPREeQ1Q
<br />
http://www.youtube.com/watch?v=GaEZgqxPHLs&feature=related
this work good to find the code ZiJRPREeQ1Q and GaEZgqxPHLs , now i want to replace all the html line with a new code
wanna to use
preg_replace
to find the whole youtube url
*
to a new code how can i do that ?
--------------adds--------------
after i get the code of youtube from url by preg_math_all
i used this code to extract the codes
foreach($match[1] as $youtube){
// $youtube; // this handle the youtube code
$match = ""; // what can i write here relative to $youtube ?
$str .= preg_replace($match, 'new code',$content); // $content handle the whole thread that contain the youtube url <a href=*>*</a>
}
the only thing that i need that what's regular expression that i can use to replace youtube code

$html = file_get_contents($url); //or curl function
$re="<link itemprop=\"embedURL\" href=\"(.+)\">";
preg_match_all("/$re/siU", $html, $matches);
$youtube = $matches[1][0];
or
$html = file_get_contents($url); //or curl function
$re="<link itemprop=\"url\" href=\"(.+)\">";
preg_match_all("/$re/siU", $html, $matches);
$youtube = $matches[1][0];

Related

How to get ID from Instagram URL using preg_match

i need to know how to preg_match ID from instagram URL
https://www.instagram.com/p/BrODgnXHlE6/?utm_source=ig_share_sheet&igshid=znsinsart176
i tried
preg_match('/https://www.instagram.com/p/(.)/(.)/U', $video_url, $matches);
$video_id = $matches[1];
But it is not working
Thank you
<?php
$url = "https://www.instagram.com/p/BrODgnXHlE6/?utm_source=ig_share_sheet&igshid=znsinsart176";
preg_match('/https:\/\/www.instagram.com\/p\/(.+)\/(.+)/U', $url, $matches);
$id = $matches[1];

text to link, problem with link that has not https

Users can add texts. This texts can have links.
I'd like do add click to it.
The problem is, some links works like:
http://www.example.com
links that has no http will not work and will become:
http://mywebsite.com/www.example.com
any ideas how to solve it?
function toLink($titulo){
$url = '~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i';
$titulo = preg_replace($url, '$0', $titulo);
return $titulo;
}
Use preg_replace_callback instead and you can interrogate the match to see if you need to add the protocol.
function toLink($titulo) {
$url = '~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i';
$titulo = preg_replace_callback($url, function($matches) {
$url = $matches[0];
if (!preg_match('/^https?:\/\//', $url)) $url = 'http://'.$matches[0];
''.$url.'';
}, $titulo);
return $titulo;
}

How to pregmatch youtube url with the youtube short url and get videoids from them php

Hi am trying to pregmatch youtube urls and youtube shorten url and get video ids from am able to pregmatch the youtube url but unable to pregmatch short url
Here is my code..
if (preg_match('#^(?:https://(?:www\\.)?youtube.com/)(watch\\?v=|v/)([a-zA-Z0-9_]*)#', $video_id, $match))
where videoid= https://www.youtube.com/watch?v=Xt4S-Ot0Jcs
How can I pregmatch short url like https://youtu.be/Xt4S-Ot0Jcs how can I get the videoids from both using php
You can try with a simpler regex if you just want to get the videoId :
if (preg_match('#(youtube\.com/watch\?v=|youtu\.be/)([a-zA-Z0-9_]*)#', $video_id, $match))
public function get_id($url)
{
$ytshorturl = 'youtu.be/';
$ytlongurl = 'www.youtube.com/watch?v=';
if (strpos($url, $ytshorturl) !== false) {
$url = str_replace($ytshorturl, $ytlongurl, $url);
}
$components = parse_url($url);
parse_str($components['query'], $results);
return $results['v'];
}
$url = "https://youtu.be/oeg192rQ1xE"; //sample url
echo "Id is : ". get_id($url);

PHP preg_match, Finding a package name from Android URL address

I need to get Android package name from the URL address.
Here is what I have done.
$url = 'https://play.google.com/store/apps/details?id=com.gamevil.projectn.global&feature=featured-apps#?t=W251bGwsMSwyLDIwMywiY29tLmdhbWV2aWwucHJvamVjdG4uZ2xvYmFsIl0.';
preg_match("~id=(\d+)~", $url, $matches);
$package_name = $matches[1];
echo $package_name;
Package name should be "com.gamevil.projectn.global"
However, my code is not working.
Is there something that I miss?
you can do this by parse_url function
<?php
$url = 'https://play.google.com/store/apps/details?id=com.gamevil.projectn.global&feature=featured-apps#?t=W251bGwsMSwyLDIwMywiY29tLmdhbWV2aWwucHJvamVjdG4uZ2xvYmFsIl0.';
$arr =parse_url($url);
$new = explode("&",$arr['query']);
$new1 = explode("=",$new[0]);
echo($new1[1] );
output
com.gamevil.projectn.global
Maybe this can help you:
$url = 'https://play.google.com/store/apps/details?id=com.gamevil.projectn.global&feature=featured-apps#?t=W251bGwsMSwyLDIwMywiY29tLmdhbWV2aWwucHJvamVjdG4uZ2xvYmFsIl0.';
preg_match("/id=(.*?)&/", $url, $matches);
$package_name = $matches[1];
echo $package_name;
preg_match will no find everything between id= and &.
But a better solution is to use parse_url to parse the url and this function will return the components of the url.

Getting the unique ID of a Viddler video from the embed code (regex/parse_url)

Not working. I think I have looked at it too long and I'm blind to the problem:
<?php
$str = $_POST['data'];
$pattern = '#(www\.|https?:\/\/){?}[a-zA-Z0-9]{2,254}\.[a-zA-Z0-9]{2,4}(\S*)#i';
if (preg_match_all($pattern, $str, $matches, PREG_PATTERN_ORDER)) {
$uri = "($matches[1])";
}
$path = parse_url($uri, PHP_URL_PATH);
$output = substr(strrchr($path, '/'), 1);
?>
Sample embed code - I need the a695c468 part of the URL - it may change in length:
<iframe id="viddler-a695c468" src="//www.viddler.com/embed/a695c468/?f=1&offset=0&autoplay=0&secret=91361932&disablebranding=0&view_secret=91361932" width="545" height="349" frameborder="0" mozallowfullscreen="true" webkitallowfullscreen="true"></iframe>
Why parse it from URL? iframe id will be faster.
preg_match_all('/"viddler-(\w+)"/', $str, $matches, PREG_PATTERN_ORDER);
Updated:
Try this
preg_match_all('/www\.viddler\.com\/embed\/(\w+)\//', $str, $matches, PREG_PATTERN_ORDER);

Categories