Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7610173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:21:21+00:00 2026-05-31T01:21:21+00:00

Hi all I have a variable that is a YouTube url that is user-generated.

  • 0

Hi all I have a variable that is a YouTube url that is user-generated. There is a lot of threads on here about Regex and what not, but most are being executed in js. I want to run everything server-side in php, here is my simple function:

function youTubeID($url){
     $url = preg_replace('http://youtu.be/ Regex goes here', '', $url);
     return $url;
}

Markup:

<a href="http://youtu.be/yhXhVMKxnkY"><img src="http://img.youtube.com/vi/'.youTubeID($variable).'/0.jpg" /><img src="play.png" alt="Watch the video" title="Watch the video"/></a>

Question:
Does anyone know the proper regex for http://youtu.be/ ?

I’m sure this is a quick answer, I’m not too savvy with regex. On a side note,does anyone know of any good resources where I can teach myself some good regex so I don’t have to ask silly syntax questions like this? Cheers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:21:22+00:00Added an answer on May 31, 2026 at 1:21 am

    Why use a (relatively slow) RegEx at all?

    $url = substr($url, strlen('http://youtu.be/'));
    

    Obviously you have a set start point so no need to calculate it each time you call the function, but doing so here to demonstrate the idea.

    Edit:

    In case there’s some reason you are required to use a RegEx:

    $url = preg_replace('#http\://youtu\.be/#', '', $url);
    

    Cheers

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a variable that's being changed all the time in the course of
I have a Class named Constants that contains all the constant variable in my
I have a variable that I would like to use in all my classes
For example, I have a variable $foo that includes all the data which I
I have a variable that I am calculating the length of and in all
I want to have one variable that contains the root path to all my
I have many joinable threads that at some point need to all stop. The
I have a variable formvar that is incremented every time a user adds an
I have a header file variable.h where i declare all my global variable.Then i
I have three classes that all have a static function called 'create'. I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.