SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot CalebMingle's Avatar
    Join Date
    Jul 2007
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_match - Youtube ID

    Well I am attempting to grab all the ID's of videos in a search on YouTube.

    I absolutely suck at patterns and regex, so I need a bit of help.

    PHP Code:
    <?php
    include "config.php";
    require_once 
    CLASSES "core.php";;
    require_once 
    CLASSES "youtube.php";

    $core = new core();
    $yt = new youtube();

    // Time to get the URL's //

    $url "http://www.youtube.com/results?search_query=fall+out+boy";

    $page file_get_contents($url);

    preg_match("/watch?v=([^*]+)/i"$page$matches);

    print_r($matches);


    ?>
    I need some help in getting all the ID's off of that page.

    Can anyone help?
    :: Caleb Mingle (@dentafrice)
    :: PHP Developer.
    :: g+: http://gplus.to/mingle

  2. #2
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    New Jersey
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to use preg_match_all(), escape the question mark, and specify what it ends with. Also better to note that it starts with /.
    PHP Code:
    preg_match_all("/\/watch\?v=(.*?)\"/i"$page$matches);
    $id_array $matches[1];
    print_r($id_array); 

  3. #3
    SitePoint Zealot CalebMingle's Avatar
    Join Date
    Jul 2007
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Although I figured it out without using preg.

    Thanks =]
    :: Caleb Mingle (@dentafrice)
    :: PHP Developer.
    :: g+: http://gplus.to/mingle

  4. #4
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    New Jersey
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually this is better, because there might be additional parameters in the URL.
    PHP Code:
    preg_match_all("/\/watch\?v=(.*?)[&\"]/i"$page$matches); 

  5. #5
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Might wanna use their API and parse the feed with simplexml: http://code.google.com/apis/youtube/...ing_for_Videos
    Saul


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •