- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP Regex to get YouTube Video ID
The parse_url and parse_str functions can be used to get the ID of a specific YouTube video.
<?php $url = " https://www.youtube.com/watch?v=VX96I7PO8YU "; parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array ); echo $my_array['v']; ?>
In the above code, the parse_url function takes in a string and slices it into an array of information. The element that the user specifically wants to work with can be specified as a second argument or the entire array can be used.
A YouTube video has an ID that can be seen in the URL. The goal is to fetch the ID after the letter ‘v’ and before ‘&’. To accomplish this, the parse_str function can be used. It is similar to GET functionality, which takes a string and creates variables that are specified by the string.
For example − If v=5R60Tpz3Bow, it creates a variable named $v. This is stored as an array element, thereby giving control over the variable being stored, so as to not overwrite it accidentally.
Note − Regular expression, also known as regex can be used to tackle many problems, but are prone to errors. Hence, they need to be used with care.
- How to download mp3 tracks from youtube video using youtube dl
- How to add a YouTube Video to your Website?
- How to play YouTube video in my Android Application?
- How android YouTube app Play Video from Intent
- How to get resource ID using get_resource_id() function in PHP and PHP 8?
- Know about YouTube and How to find Free Movies on YouTube?
- How to get programmatically android build id?
- How to get device id in android?
- Java Program to get timezone id strings
- How to Bypass YouTube ADs
- How to get programmatically android device secure id?
- How to get current thread id in android?
- Split string into sentences using regex in PHP
- What does [Ss]* mean in regex in PHP?