Regex for youtube channel URL - php

how can I validate youtube channel URL using REGEX ?
I found this pattern but it doesn't work properly
/((http|https):\/\/|)(www.|)youtube\.com\/(channel\/|user\/|)[a-zA-Z0-9]{1,}/
Can anyone help me ?

Your problem is the extra pipe after user\/
Here is the corrected regex:
((http|https):\/\/|)(www\.|)youtube\.com\/(channel\/|user\/)[a-zA-Z0-9_-]{1,}
The reason this is a problem is because it make (channel|user) optional.
A better way to write this regex is
(https?:\/\/)?(www\.)?youtube\.com\/(channel|user)\/[\w-]+

After some in-depth research, I came up with the following RegEx:
^https?:\/\/(www\.)?youtube\.com\/(channel\/UC[\w-]{21}[AQgw]|(c\/|user\/)?[\w-]+)$
It allows:
https or http
www. or not
channel/ URLs (since 2015)
user/ URLs (legacy)
custom (c/) URLs (since 2016)
short URLs (user/ or c/ removed)
It also validates channel IDs since they follow a pattern:
Start with UC
21 characters of [0-9a-zA-Z_-] (same as [\w-])
end with one of [AQgw]
Tested with:
https://www.youtube.com/channel/UCARj2eHnsYMuCZDmYZ5q4_g
https://www.youtube.com/channel/UCUZHFZ9jIKrLroW8LcyJEQQ
https://www.youtube.com/c/YouTubeCreators
https://www.youtube.com/YouTubeCreators
https://www.youtube.com/user/partnersupport
https://www.youtube.com/partnersupport
http://www.youtube.com/partnersupport
https://youtube.com/partnersupport
http://youtube.com/partnersupport
Sources:
https://support.google.com/youtube/answer/6180214
https://breadnbeyond.com/youtube-marketing/youtube-custom-channel-url/
https://webapps.stackexchange.com/a/101153

To get the channel name or channel id from a youtube URL use:
(?:https|http)\:\/\/(?:[\w]+\.)?youtube\.com\/(?:c\/|channel\/|user\/)?([a-zA-Z0-9\-]{1,})
Works for:
https://www.youtube.com/user/channelblabla
https://www.youtube.com/channel/channelblabla
https://www.youtube.com/c/channelblabla
https://www.youtube.com/channelblabla
Channel ID's start with 'UC'. I don't know of any other way to recognize channel ID's vs. channel names.

I found the best solution for me is
/(https?:\/\/)?(www\.)?youtu((\.be)|(be\..{2,5}))\/((user)|(channel))\/?([a-zA-Z0-9\-_]{1,})/
This works for
http://www.youtube.com/channel/uc_fglsfl
http://youtube.co.uk/channel/asdasgfgjd
https://youtube.com/channel/ghjgk+öää,
https://youtube.net/channel/43568&gsldkfj
https://youtube.de/channel/dtgzu&&dadg
http://youtube.com/channel/vgujsgh&as=gr
http://youtube.com/channel/xdfhxfgu
let str = 'http://www.youtube.com/channel/uc_fglsfl'
let pattern = '/(https?:\/\/)?(www\.)?youtu((\.be)|(be\..{2,5}))\/((user)|(channel))\/?([a-zA-Z0-9\-_]{1,})/'
let matchs = str.match(pattern)
//result id
matchs[9]

Related

How to format URL with query parameters for IGDB API?

here is the documentation for the database I'm trying to use: https://www.igdb.com/api/v1/documentation
I requested a token from them and it was granted.
This URL works for me (I've written "redacted" in): https://www.igdb.com/api/v1/games?token=redacted
and it returns the following:
{"games":[{"id":1,"name":"Thief II: The Metal Age","slug":"thief-ii-the-metal-age","release_date":"2000-03-21","alternative_name":null},{"id":2,"name":"Thief: The Dark Project","slug":"thief-the-dark-project","release_date":"1998-11-30","alternative_name":null},{"id":3,"name":"Thief: Deadly Shadows","slug":"thief-deadly-shadows","release_date":"2004-05-25","alternative_name":null},{"id":4,"name":"Thief","slug":"thief","release_date":"2014-02-25","alternative_name":"Thief 4"},{"id":5,"name":"Baldur's Gate","slug":"baldur-s-gate","release_date":"1998-11-30","alternative_name":null},{"id":6,"name":"Baldur's Gate II: Shadows Of Amn","slug":"baldur-s-gate-ii-shadows-of-amn","release_date":"2000-09-24","alternative_name":null},{"id":7,"name":"Jagged Alliance","slug":"jagged-alliance","release_date":"1994-06-02","alternative_name":null},{"id":8,"name":"Jagged Alliance: Deadly Games","slug":"jagged-alliance-deadly-games","release_date":"1995-01-01","alternative_name":null},{"id":9,"name":"Jagged Alliance 2","slug":"jagged-alliance-2","release_date":"1999-07-23","alternative_name":null},{"id":10,"name":"Jade Empire: Special Edition","slug":"jade-empire-special-edition","release_date":"2005-04-12","alternative_name":null},{"id":11,"name":"Vampire: The Masquerade - Bloodlines","slug":"vampire-the-masquerade-bloodlines","release_date":"2004-11-16","alternative_name":null},{"id":12,"name":"Vampire: The Masquerade - Redemption","slug":"vampire-the-masquerade-redemption","release_date":"2000-06-07","alternative_name":null},{"id":13,"name":"Fallout","slug":"fallout","release_date":"1997-09-30","alternative_name":null},{"id":14,"name":"Fallout 2","slug":"fallout-2","release_date":"1998-09-30","alternative_name":null},{"id":15,"name":"Fallout 3","slug":"fallout-3","release_date":"2008-10-28","alternative_name":null},{"id":16,"name":"Fallout: New Vegas","slug":"fallout-new-vegas","release_date":"2010-10-19","alternative_name":null},{"id":17,"name":"Fallout Tactics: Brotherhood Of Steel","slug":"fallout-tactics-brotherhood-of-steel","release_date":"2001-03-15","alternative_name":null},{"id":18,"name":"Max Payne","slug":"max-payne","release_date":"2001-07-23","alternative_name":null},{"id":19,"name":"Max Payne 2: The Fall Of Max Payne","slug":"max-payne-2-the-fall-of-max-payne","release_date":"2003-10-24","alternative_name":null},{"id":20,"name":"BioShock","slug":"bioshock","release_date":"2007-08-21","alternative_name":null},{"id":21,"name":"BioShock 2","slug":"bioshock-2","release_date":"2010-02-09","alternative_name":null},{"id":22,"name":"System Shock 2","slug":"system-shock-2","release_date":"1999-08-11","alternative_name":null},{"id":23,"name":"System Shock","slug":"system-shock","release_date":"1994-09-22","alternative_name":null},{"id":24,"name":"UFO: Enemy Unknown","slug":"ufo-enemy-unknown--1","release_date":"1994-01-01","alternative_name":null},{"id":25,"name":"X-COM: Terror From The Deep","slug":"x-com-terror-from-the-deep","release_date":"1995-01-01","alternative_name":null}]}
What I'm trying to do is figure out how to search by title. For example, if I wanted only games that have the term "Zelda" in it, how would I format the URL properly?
I've tried:
https://www.igdb.com/api/v1/games&q=zelda?token=redacted
which returns "forbidden".
I've also tried
https://www.igdb.com/api/v1/games?token=redacted&q=zelda
which returns that same exact list of games above, as if completely unaffected by the "Zelda" term.
Can anyone give advice on how to properly format this URL for performing a search based on Game Title? Thank you!
The first query parameter should be passed with a "?" and the following ones each with "&".
The documentation of the API says that you should query /games/search instead of /games directly. Please try this.

Get YouTube ID using JavaScript .match()

So I have a working preg_match in PHP, however, for the life of me, I cannot get the same function to work using Javascript/jQuery.
This is what I am stuck on currently:
yt=$('#yt').val().match(/~^\(?:https?://\)?(?:www\.)?(?:youtube\.com|youtu\.be)(?:/)(?:watch\?v=)?([^&]+)~x/);
alert(yt[1]);
This is the working function in PHP:
$rx = "~"
."^(?:https?://)?" // Optional protocol
."(?:www\.)? " // Optional subdomain
."(?:youtube\.com|youtu\.be)" // Mandatory domain name
."(?:/)" //mandatory bracket
."(?:watch\?v=)?" //optional URI
."([^&]+)" //video id as capture group 1
."~x";
$has_match = preg_match($rx, $url, $matches);
Any idea how to get this functioning?
I found some similar posts on Stack, but they are far less complex than this regex, and couldnt get my head wrapped around the differences.
Not 100% sure but I think you haven't escaped everything correctly.
yt=$('#yt').val().match("^(?:https?://)?(?:www\.)?(?:youtube\.com|youtu\.be)(?:/)(?:watch\?v=)?([^&]+)")
alert(yt[1]);
"https://www.youtube.com/watch?v=dQw4w9WgXcQ".match("^(?:https?://)?(?:www\.)?(?:youtube\.com|youtu\.be)(?:/)(?:watch\?v=)?([^&]+)");
results in
["https://www.youtube.com/watch?v=iQbS-8m3svw", "watch?v=dQw4w9WgXcQ"]

PHP check if href is a file

I am using a script to check links on a given page. I am using simple html DOM to parse the information into an array. I have to check the href of all the a tags to find if they contain a file or something like # or JS.
I tried the following without success.
if(preg_match("|^(.*)|iU", $href)){
save_link();
}
I dont know it my pattern is wrong or if there is a better method to complete this function.
I want to be able to detect if $href contains .com .php .file extensions. This way it will filter out items like # "function()" and other items used in the href attribute.
EDIT:
parse_url will not work stop posting it. The value # returns as a valid url like I stated above I am trying to look for any string followed by .* with no more than 4 chars following the .
I believe that the function you're looking for is parse_url().
This function will take a URL string, and return an array of components, which will allow you to work out what kind of URL it is.
However note that it has issues with incomplete URLs in PHP versions prior to 5.4.7, so you need to have the very latest PHP to get the best out of it.
Hope that helps.
See http://php.net/manual/en/function.parse-url.php
I'm assuming you don't want to match fragments (#) because you are not concerned with following internal anchors.
parse_url breaks up the different parts of the url into an array. You can see the path component of the URL in this array and run your check against that.
You can use parse_url() , like this :
$res = parse_url($href);
if ( $res['scheme'] == 'http' || $res['scheme'] == 'https'){
//valid url
save_link();
}
UPDATE:
I've added code to filter only http and https urls, thanks to Baba for spotting this.

Parsing Wiki API content

I have this wiki from the API http://fr.wikipedia.org/w/api.php?action=query&titles=%C9rythropo%EF%E9tine&prop=revisions&rvprop=content&format=xmlfm
which I would like to retrieve the main content starting from:
L''''érythropoïétine''' ('''EPO''') est une [[hormone]] ......etc
I tried for a start to preg_replace everything from the top starting from the word "{{Chimiebox..." to the bottom "}}" using this
preg_replace( '/^{{(.*)}}$/sim', '', $value[0]['*'] );
But kind of doesn't work..does anyone know of a good way to determine the start of the content?? Thanks for any advice.
Well, afaik the most projects use the Wikipedia Parser directly, e.g. the Wikipedia Offline Client Project at my university. Since you seem to be using php, this may the be the easiest way for you.

How to encode # character in the url + php + Joomla

I am trying to fetch a variable address from my current URL using JRequest::getVar('address') method.
But if the address value has a (#) character, the part after the # character is not retrieved.
I understand that URI is a combination of query + fragment and the part after a hash symbol is treated as a fragment.
I have tried to use urlencode method but it still doesn't solve the problem.
Can anyone please tell me how to solve the issue?
What is the problem with using urlencode? It should replace # with %23 and all should be well. You can try JRequest::getVar(str_replace('#', '%23', 'address')) which should do the trick. Can you post an example URL that doesn't get properly urlencoded?
I guess you will have to replace the hash-symbol on your own. For example:
str_replace($the_url, '#', '-');
I don't know, where exactly you have to do that, because I don't know how the Joomla!-Framework handles links and urls. But I am sure, that someone else can help here any further...
Encode the Hash in the URL with a %23 replacement
http://twitter.com/home?status=I+believe+in+%23love
"I believe in #love"
The part after # is never sent to Apache/PHP, and can therefore not be retrieved by a PHP script. What you need to do, is to url encode the ADDRESS parameter of the URL.
test.com/index.php?ADDRESS=<?= urlencode('101 Street #6 City') ?>
That code will generate the following url
test.com/index.php?ADDRESS=101+Street+%236+City
Now on this URL, you can retrieve address with JRequest::getVar('ADDRESS')
Check this Joomla doc out. You can retrieve what Joomla call the 'fragment' by doing:
$uri = 'http://fredbloggs:itsasecret#www.example.com:8080/path/to/Joomla/index.php?task=view&id=32#anchorthis';
$u =& JURI::getInstance( $uri );
echo 'Fragment is ' . $u->getFragment();

Categories