parse youtube video id using preg_match
This regex grabs the ID from all of the various URLs I could find...
There may be more out there, but I couldn't find reference of them anywhere. If you come across one this doesn't match, please leave a comment with the URL, and I'll try and update the regex to match your URL.
if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/\s]{11})%i', $url, $match)) {
$video_id = $match[1];
}
Here is a sample of the URLs this regex matches: (there can be more content after the given URL that will be ignored)
- http://youtu.be/dQw4w9WgXcQ ...
- http://www.youtube.com/embed/dQw4w9WgXcQ ...
- http://www.youtube.com/watch?v=dQw4w9WgXcQ ...
- http://www.youtube.com/?v=dQw4w9WgXcQ ...
- http://www.youtube.com/v/dQw4w9WgXcQ ...
- http://www.youtube.com/e/dQw4w9WgXcQ ...
- http://www.youtube.com/user/username#p/u/11/dQw4w9WgXcQ ...
- http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/dQw4w9WgXcQ ...
- http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ ...
- http://www.youtube.com/?feature=player_embedded&v=dQw4w9WgXcQ ...
It also works on the youtube-nocookie.com URL with the same above options.
It will also pull the ID from the URL in an embed code (both iframe and object tags)
PHP Regex to get youtube video ID?
Use parse_url() and parse_str().
(You can use regexes for just about anything, but they are very easy to make an error in, so if there are PHP functions specifically for what you are trying to accomplish, use those.)
parse_url takes a string and cuts it up into an array that has a bunch of info. You can work with this array, or you can specify the one item you want as a second argument. In this case we're interested in the query, which is PHP_URL_QUERY
.
Now we have the query, which is v=C4kxS1ksqtw&feature=relate
, but we only want the part after v=
. For this we turn to parse_str
which basically works like GET
on a string. It takes a string and creates the variables specified in the string. In this case $v
and $feature
is created. We're only interested in $v
.
To be safe, you don't want to just store all the variables from the parse_url
in your namespace (see mellowsoon's comment). Instead store the variables as elements of an array, so that you have control over what variables you are storing, and you cannot accidentally overwrite an existing variable.
Putting everything together, we have:
<?php
$url = "http://www.youtube.com/watch?v=C4kxS1ksqtw&feature=relate";
parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_vars );
echo $my_array_of_vars['v'];
// Output: C4kxS1ksqtw
?>
Working example
Edit:
hehe - thanks Charles. That made me laugh, I've never seen the Zawinski quote before:
Some people, when confronted with a problem, think ‘I know, I’ll use regular expressions.’ Now they have two problems.
– Jamie Zawinski
php- parsing youtube video id
Just get everything after = and before ]. Like
/\[youtube=(.+)\]/
PHP: preg_match_all Youtube video IDs from text
To expand on my comment, you're replacing the result text each time with the original string, $sample_text. This is a simple fix, just initialise $processed_text at the start, and work on that.
function regex($sample_text) {
$processed_text = $sample_text;
if (preg_match_all('#(?:https?:\/\/)?(?:m\.|www\.)?(?:youtu\.be\/|youtube\-nocookie\.com\/embed\/|youtube\.com\/(?:embed\/|v\/|e\/|\?v=|shared\?ci=|watch\?v=|watch\?.+&v=))([-_A-Za-z0-9]{10}[AEIMQUYcgkosw048])(.*?)\b#s', $sample_text, $matches, PREG_SET_ORDER)) {
print_r($matches);
foreach ($matches as $match) {
$add = ' (here)';
$processed_text = str_replace($match[0], $match[0] . $add, $processed_text);
}
}
return $processed_text;
}
echo regex($sample_test);
Your regex is also not matching to the end of the URL. For the purposes of the sample text you provided, you could match up to anything that isn't whitespace:
'#(?:https?:\/\/)?(?:m\.|www\.)?(?:youtu\.be\/|youtube\-nocookie\.com\/embed\/|youtube\.com\/(?:embed\/|v\/|e\/|\?v=|shared\?ci=|watch\?v=|watch\?.+&v=))([-_A-Za-z0-9]{10}[AEIMQUYcgkosw048])\S*#s'
However this won't match characters like "
or .
, but you could add those in as an |
in a group. You seem to have a pretty good grasp of regex, so I'll assume you can work this out - if not, comment and I'll update my answer.
For completeness sake, I've included the completed code with my regex:
function regex($sample_text) {
$processed_text = $sample_text;
if (preg_match_all('#(?:https?:\/\/)?(?:m\.|www\.)?(?:youtu\.be\/|youtube\-nocookie\.com\/embed\/|youtube\.com\/(?:embed\/|v\/|e\/|\?v=|shared\?ci=|watch\?v=|watch\?.+&v=))([-_A-Za-z0-9]{10}[AEIMQUYcgkosw048])\S*#s', $sample_text, $matches, PREG_SET_ORDER)) {
print_r($matches);
foreach ($matches as $match) {
$add = ' (here)';
$processed_text = str_replace($match[0], $match[0] . $add, $processed_text);
}
}
return $processed_text;
}
echo regex($sample_test);
Parse a message using php replacing youtube URL for youtube video ID
First you would match a valid URL, then extract a valid YouTube ID from that URL, then replace the original URL found with the matching ID (if a valid ID was found):
<?php
$message = "
this is a youtube video http://www.youtube.com/watch?v=w6yF_UV1n1o&feature=fvst i want only the id
this is not a youtube video http://google.com do nothing
this is an youtube video http://www.youtube.com/watch?v=w6yF_UV1n1o&feature=fvst i want only the id
";
preg_match_all('#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#', $message, $matches);
if (isset($matches[0]))
{
foreach ($matches[0] AS $url)
{
if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $matches))
$message = str_replace($url, $matches[1], $message);
}
}
echo $message;
Source: http://daringfireball.net/2009/11/liberal_regex_for_matching_urls & https://stackoverflow.com/a/6382259/1748964
pattern to get video id from youtube URL
Here is the solution :
^# Match any youtube URL
(?:https?://)? # Optional scheme. Either http or https
(?:
www\. # Optional www subdomain
| m\. # Optional mobile subdomain
)?
(?: # Group host alternatives
youtu\.be/ # Either youtu.be,
| youtube\.com # or youtube.com
(?: # Group path alternatives
/ # Shortlink
| /embed/ # Either /embed/
| /v/ # or /v/
| /&v=/ # or ?feature=youtu.be&v=NXwxHU2Q0bo
| /watch\?v= # or /watch\?v=
| /watch\?feature=youtu\.be&v= # alternativ link with watch
) # End path alternatives.
) # End host alternatives.
([\w-]{10,12}) # Allow 10-12 for 11 char youtube id.
$
https://regex101.com/r/LiCquP/2
https://regex101.com/r/LiCquP/3
For the test :)
RegEx pattern to get the YouTube video ID from any YouTube URL
if (preg_match('/youtube\.com\/watch\?v=([^\&\?\/]+)/', $url, $id)) {
$values = $id[1];
} else if (preg_match('/youtube\.com\/embed\/([^\&\?\/]+)/', $url, $id)) {
$values = $id[1];
} else if (preg_match('/youtube\.com\/v\/([^\&\?\/]+)/', $url, $id)) {
$values = $id[1];
} else if (preg_match('/youtu\.be\/([^\&\?\/]+)/', $url, $id)) {
$values = $id[1];
}
else if (preg_match('/youtube\.com\/verify_age\?next_url=\/watch%3Fv%3D([^\&\?\/]+)/', $url, $id)) {
$values = $id[1];
} else {
// not an youtube video
}
This is what I use to extract the id from an youtube url. I think it works in all cases.
Note that at the end $values = id of the video
How to extract YouTube ID from this Url
Decode it first and use the second index ([1]
) of array matches
:
if(isset($_GET["q"]))
{
$url = urldecode(rawurldecode($_GET["q"]));
# https://www.youtube.com/watch?v=nn5hCEMyE-E
preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $url, $matches);
echo $matches[1];
# nn5hCEMyE-E
}
Related Topics
"Connect Failed: Access Denied For User 'Root'@'Localhost' (Using Password: Yes)" from PHP Function
How to Call a PHP Function on the Click of a Button
How to Push Both Value and Key into PHP Array
PHP - Get Bool to Echo False When False
Submitting a Multidimensional Array Via Post With PHP
Remove All Attributes from HTML Tags
What Are the Differences in Die() and Exit() in PHP
Symfony3 Classnotfoundexception After Bundle Creation
Warning About '$Http_Raw_Post_Data' Being Deprecated
How Does Recursiveiteratoriterator Work in PHP
Get PHP to Stop Replacing '.' Characters in $_Get or $_Post Arrays
PHP Regex to Get Youtube Video Id
How to Select and Upload Multiple Files With HTML and PHP, Using Http Post
List All the Files and Folders in a Directory With PHP Recursive Function
Using an Array as Needles in Strpos