Created
January 15, 2020 10:46
-
-
Save kernusr/dc34e52afda7d33d01d02e82bdebe9dd to your computer and use it in GitHub Desktop.
Regular Expression to search for all known links to YouTube embedded in the HTML document code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$re = '~<a\s+(?:[^>]*?\s+)?href=(["\'])(https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube(?:-nocookie)?\.com\S*?[^\w\s-])([\w-]{11})(?=[^\w-]|$)[?=&+%\w.-]*)\1~ix'; | |
$str = '<a href="http://youtu.be/NLqAF9hrVbY">asd</a> | |
<a href="http://www.youtube.com/embed/NLqAF9hrVbY">asd</a> | |
<a href="https://www.youtube.com/embed/NLqAF9hrVbY">asd</a> | |
<a href="http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US">asd</a> | |
<a href="http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US">asd</a> | |
<a href="http://www.youtube.com/watch?v=NLqAF9hrVbY">asd</a> | |
<a href="http://www.youtube-nocookie.com/NLqAF9hrVbY">asd</a> | |
<a href="http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo">asd</a> | |
<a href="http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I">asd</a> | |
<a href="http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/2/PPS-8DMrAn4">asd</a> | |
<a href="http://gdata.youtube.com/feeds/api/videos/NLqAF9hrVbY">asd</a> | |
<a href="http://www.youtube.com/watch?v=spDj54kf-vY&feature=g-vrec">asd</a> | |
<a href="http://www.youtube.com/watch?v=spDj54kf-vY&feature=youtu.be">asd</a> | |
'; | |
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0); | |
// Print the entire match result | |
var_dump($matches); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment