How to extract YouTube video ID from a YouTube link
In this Bubble tutorial we demonstrate how to extract a YouTube video ID from a full YouTube URL. This video includes how to factor for YouTube shortened links and the presence of additional data (query strings) within the YouTube URL.
Unlock YouTube's hidden data: Learn how to extract video IDs from any URL format with our Bubble YouTube tutorial!
Master Bubble's find and replace: Discover how to handle multiple URL formats and extract crucial data using truncate functions.
Boost your Bubble skills: Extract YouTube IDs like a pro, even with tricky query strings!
Extracting YouTube Video IDs from URLs
In this quick Bubble tutorial, I'm going to show you how to extract a YouTube video's ID from a YouTube URL. This can be useful if you're embedding YouTube videos, depending on how you store that data in your database.
Using Find and Replace
One way to go about this is to use the find and replace function. I'm going to say input input a to refer to my input field value and then use find and replace. This is such a handy trick where you can find some text in a string in the input field and replace it with nothing.
Handling Different URL Formats
We have to cover many different eventualities as we can't always rely on our users inputting data in the same format. For example, what happens if they use the share URL, which is YouTube's link shortener? Let's copy that into here, and you'll see that it doesn't work. But what we can do is stack our find and replaces.
Dealing with Query Strings
There is another eventuality to cover, and that is if the URL has got some sort of query string at the end. For example, if we jump midway through the video and then say start from now, this is a query string related to time. But there could be other things to do with tracking and the source where people have arrived at the website from.
Using Truncate for Consistency
There's one final thing that I'd add, which is to truncate. Truncate means to trim our text, and YouTube video IDs are all 11 characters long. There's a really fascinating video by Tom Scott which we can link to, saying how YouTube is really unlikely to run out of video IDs - the math on it is astonishing.
Putting It All Together
We can say truncate to 11, so now if I paste in with the query string, it still only sticks to the first 11 characters of what's remained after we've done the find and replace. Let's just check this with our full URL. I'll add a query string here, and it still works because it removes everything in front of it, leaving only what's after this part of the URL. Then we can use truncate to limit what we return to just 11 characters.
Conclusion
This method allows us to extract YouTube video IDs consistently, regardless of the URL format or additional parameters. It's a useful technique when working with YouTube videos in your Bubble app.
Can't find what you're looking for?
Search our 300+ Bubble tutorial videos. Start learning no code today!
Flexible Pricing Plans to Fit Your No-Code Journey
Choose the plan that aligns with your goals and start building your startup today.
Have questions?
We have answers!
Find answers to common questions about our membership plans, programs, and more.
Both plans offer full access to our learning resources, community, and support. The Annual plan provides a significant discount (over 15%) compared to paying monthly, and it allows you to lock in your rate for a full year.
Absolutely! You can easily upgrade or downgrade your membership plan at any time by logging into your account and selecting the desired plan. Any unused portion of your current plan will be prorated and applied to your new plan.
As a Planet No Code member, you'll receive a discount on our Bubble coaching sessions. Monthly members receive a 10% discount, while Annual members receive a 17.5% discount. To redeem your discount, simply log into your account and book a coaching session through our platform.
Our 8-week intensive mentorship program is designed to provide personalized guidance and support to help you accelerate your startup journey. You'll be matched with a startup expert who will work with you one-on-one to set goals, overcome challenges, and make rapid progress.
To apply for the Mastery Program, simply click the "Request Invitation" button on our pricing page and fill out the application form. Our team will review your application and schedule a call with you to discuss your goals and determine if the program is a good fit for your needs.
We accept all major credit cards, including Visa, Mastercard, American Express, and Discover.
While we don't offer a free trial, we do provide a 14-day money-back guarantee. If you're not completely satisfied with your membership within the first 14 days, simply contact our support team, and we'll issue a full refund.
If you decide that Planet No Code isn't the right fit for you, you can cancel your membership at any time by logging into your account and navigating to the subscription management page. Click the "Cancel Membership" button, and your membership will be terminated at the end of your current billing cycle.