Change language

How to get YouTube video id using PHP Regex?

|
YouTube ID examples from the link below:
  • https://youtu.be/hjGD08xfg9c
  • https://www.youtube.com/watch?v=hjGD08xfg9c
  • https://www.youtube.com/watch?vi=hjGD08xfg9c
  • https://www.youtube.com/?v=hjGD08xfg9c
  • https://www.youtube.com/?vi=hjGD08xfg9c
All of these URLs contain the string "hjGD08xfg9c" is a YouTube ID. Now, using this knowledge, we can create a regular expression to get the YouTube ID from a given link in PHP. Regex: Now we know that there are five basic formats in which we can get YouTube id, i.e. using v = or vi = or v / or vi / or youtu.be/. Since the request starts with ’? ’ or ’ yout.be / & # 39 ;, start regex with ’? ’ or search yout.be/. It will ignore the part of the URL before ’? ’ or "yout.be/". After that search for "v =" or "vi =", save the next 11 characters and print it.According to this logic, the regexp would be
preg_match_all ("# (? & Lt; = v = | v / | vi = | vi / | youtu.be /) [a-zA-Z0-9 _-] {11} # ", $url, $match);
Example.  this example will show a YouTube ID with a YouTube link as input. $url = ’https: // youtu.be/hjGD08xfg9c https: // www.youtube.com/watch?v=hjGD08xfg9c https: // www.youtube.com/watch?vi=hjGD08xfg9c https: // www.youtube.com/?v=hjGD08xfg9c https: // www.youtube.com/?vi=hjGD08xfg9c & # 39 ;; preg_match_all ( "# (? < = v = | v / | vi = | vi / | youtu.be /) [a-zA-Z0-9 _-] {11} # " , $url , $matches ); print_r ( $matches [0]);  
?>
Exit:
Array ([ 0] = > hjGD08xfg9c [1] = > hjGD08xfg9c [2] = > hjGD08xfg9c [3] = > hjGD08xfg9c [4] = > hjGD08x> 



                   

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method