Jump to content

Curl - preg_match_all - string replace?


Go to solution Solved by Ch0cu3r,

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

  • Solution

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 />";    
        }
    }
Edited by Ch0cu3r
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.