Jump to content

Curl - preg_match_all - string replace?


Bleucube

Recommended Posts

Howdy.

 

First time poster, newbie coder.  I've scanned the forums for the last couple hours and although there are some close answers - nothing works nor accomplishes my job.

 

My goal:

Scrape a website, find all youtube links, create a page with all videos found via iframe.

 

My Issue:
My code below works great, but all the URLS look like http://www.youtube.com/watch?v=xxxxxxxxxxx

Not sure what to do.  Should I use str_replace to change it from that to http://www.youtube.com/embed/xxxxxxxx  ?

 

Here's the code:

<?php

    $curl = curl_init();
    curl_setopt ($curl, CURLOPT_URL, "http://xxxxx");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec ($curl);
    curl_close ($curl);

        //links
    if(preg_match_all("/(http\:\/\/www\.youtube\.com\/watch\?v=\w{11})/", $result, $links))
    {
        foreach($links[0] as $link)
        {
            echo $link."<br />";    
        }
    }

?> 

Thanks!

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/286682-curl-preg_match_all-string-replace/
Share on other sites

No, just alter the regex to only capture the value of v= not the whole of the youtube url

 if(preg_match_all("/http\:\/\/www\.youtube\.com\/watch\?v=(\w{11})/", $result, $links))
    {
        foreach($links[1] as $link)
        {
            echo "http://youtube.com/embed/$link<br />";    
        }
    }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.