Android-wd
Android-wd

Reputation: 41

Get YouTube link from content

I have content with a string and some images and some video, and I need to get only the video in this content. How can I do that?

I tried it the following way, but sometimes I do not get video and sometimes it gets me a video link:

<?php

    $string = $post->post_content;

    $pattern = '/(http\S*(?=(?:&|\?)rel))/im';

    $array = array();

    preg_match($pattern, $string, $array);

    $link = $array[0];

    print '<video>'.$link.'</video>';

?>

The example of my content is like:

    Quinquaginta septem est – pars tua, triginta quinque millia. Est autem extra plus quindecim, tota tua est, quom meruisset. Fac nos fecit. SIC. Puto quia una res potest – venimus in cognitionem. Vide pretium in…

    https://www.youtube.com/watch?t=110&v=1MBWZWyvMng

    Respice … Sentio sicut ego vobis exponam: sed vias breve iterum conabor. Fugere hoc maior difficultas est nobis. Perdet eam batch nostri. Et delebis eam ac omnia opus est vestigium, ut possimus coques. Sin id, quod morte morieris. Locus non est peccatum. Nec apud hos.

 [youtube https://www.youtube.com/watch?t=110&v=1MBWZWyvMng]

Upvotes: 0

Views: 162

Answers (2)

Bernhard
Bernhard

Reputation: 1870

a simple

preg_match_all('/https?\:\/\/(?:www\.)?(?:youtube\.com\/[^\s]*v\=|youtu\.be\/)([a-z0-9]+)/is', $input, $matches);

will output all youtube-links in your string. $input is the datasource $matches will return the matches in an array.

Upvotes: 1

walid toumi
walid toumi

Reputation: 2272

youtube[^\] ]+

Test this simple pattern

Upvotes: 0

Related Questions