У меня есть строка вроде этого:
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, https://www.youtube.com/watch?v=7TL02DA5MZM when an unknown printer took a galley of type and scrambled it to make a type Это то, что у меня есть:
 preg_match("(?:http://)?(?:www.)?(?:youtube.com|youtu.be)/(?:watch\?)?([^\s]+?)", $content, $m); var_dump( $m ); 
и хотите извлечь ссылку на youtube. Идентификатор видео тоже будет в порядке.
Не помогите!
Это сработает для вас,
 \S*\bwww\.youtube\.com\S* 
  \S* соответствует нулю или нескольким символам без пробела. 
Код будет,
 preg_match('~\S*\bwww\.youtube\.com\S*~', $str, $matches); 
DEMO
И я внес некоторые исправления в исходное регулярное выражение.
 (?:https?://)?(?:www.)?(?:youtube.com|youtu.be)/(?:watch\?v=)?([^\s]+) 
DEMO
 $str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, https://www.youtube.com/watch?v=7TL02DA5MZM when an unknown printer took a galley of type and scrambled it to make a type"; preg_match('~(?:https?://)?(?:www.)?(?:youtube.com|youtu.be)/(?:watch\?v=)?([^\s]+)~', $str, $match); print_r($match); 
Вывод:
 Array ( [0] => https://www.youtube.com/watch?v=7TL02DA5MZM [1] => 7TL02DA5MZM ) 
 (?:https?:\/\/)?www\.youtube\.com\S+?v=\K\S+ 
  Вы можете получить идентификатор видео, сопоставляя URL-адрес youtube, а затем отбрасывая с помощью \K .See demo. 
https://regex101.com/r/tX2bH4/21
 $re = "/(?:https?:\\/\\/)?www\\.youtube\\.com\\S+?v=\\K\\S+/i"; $str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, https://www.youtube.com/watch?v=7TL02DA5MZM when an unknown printer took a galley of type and scrambled it to make a type"; preg_match_all($re, $str, $matches); 
У меня появилось следующее регулярное выражение:
 https?:\/\/(w{3}\.)?youtube\.com\/watch\?.+?(\s|$) 
Вот как я использую это:
 $str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, https://www.youtube.com/watch?v=7TL02DA5MZM when an unknown printer took a galley of type and scrambled it to make a type"; preg_match("/https?:\/\/(w{3}\.)?youtube\.com\/watch\?.+?(\s|$)/", $str, $matches); $ytube = $matches[0]; $parse = parse_url($ytube); parse_str($parse["query"], $query); echo $ytube; print_r($parse); print_r($query); 
И вот вывод элементов:
 https://www.youtube.com/watch?v=7TL02DA5MZM Array ( [scheme] => https [host] => www.youtube.com [path] => /watch [query] => v=7TL02DA5MZM ) Array ( [v] => 7TL02DA5MZM )