Mostafa King
Mostafa King

Reputation: 103

preg_match and preg_replace for youtube url

i have this code

preg_match_all('%(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $asd, $match);

to find youtube key of urls like

<a href="http://www.youtube.com/watch?v=">http://www.youtube.com/watch?v=ZiJRPREeQ1Q</a>
         <br />
         <a href="http://www.youtube.com/watch?v=GaEZgqxPHLs&feature=related">http://www.youtube.com/watch?v=GaEZgqxPHLs&feature=related</a>

this work good to find the code ZiJRPREeQ1Q and GaEZgqxPHLs , now i want to replace all the html line with a new code

wanna to use

preg_replace

to find the whole youtube url

<a href="*">*</a> 

to a new code how can i do that ?

--------------adds--------------

after i get the code of youtube from url by preg_math_all i used this code to extract the codes

foreach($match[1] as $youtube){
           // $youtube; // this handle the youtube code
           $match = "";  // what can i write here relative to $youtube ?
          $str .= preg_replace($match, 'new code',$content); // $content handle the whole thread that contain the youtube url <a href=*>*</a>                                  
         }

the only thing that i need that what's regular expression that i can use to replace youtube code

Upvotes: 0

Views: 1009

Answers (1)

Ali Demirci
Ali Demirci

Reputation: 5442

$html = file_get_contents($url); //or curl function     
$re="<link itemprop=\"embedURL\" href=\"(.+)\">";
preg_match_all("/$re/siU", $html, $matches);
$youtube = $matches[1][0];

or

$html = file_get_contents($url); //or curl function     
$re="<link itemprop=\"url\" href=\"(.+)\">";
preg_match_all("/$re/siU", $html, $matches);
$youtube = $matches[1][0];

Upvotes: 1

Related Questions