SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict Owz2004's Avatar
    Join Date
    Mar 2004
    Location
    U.K (Wales)
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using regular Expressions to strip Youtube URL from Movie ID

    Hi I have a site which allows users to enter their details along with a youtube video link. This then gets added to a database. I want a page which displays all the submitted videos, using the Youtube API..

    I've worked out how to pull the Videoentry meta data to create thumbnails for a video.. but I want to grab the full url which the user entered from my database.. run it through some regular expressions to get rid of the http://www.youtube.com/watch?v=TF3fCM0c7xE(red part) and store the id in a variable. I want to then call the meta data based on the video id using the:

    $videoEntry = $yt->getVideoEntry('TF3fCM0c7xE');

    Can anyone help me with this?

    Thank you

  2. #2
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,615
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    You can use this:

    http://www.php.net/manual/en/function.parse-url.php

    And use explode() with the & symbol on the query section of the URL.

    Then, search exploded array for 'v=' and retrieve the value following 'v=' using substr().
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  3. #3
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Force Flow, good suggestion with parse_url() to get the query string. However, why explode/substr when you could make good use of parse_str()?

    PHP Code:
    $url 'http://www.youtube.com/watch?v=TF3fCM0c7xE';
    parse_str(parse_url($urlPHP_URL_QUERY), $query);
    $video_id = isset($query['v']) ? $query['v'] : NULL
    Of course, Owz2004, if you really want/need a regular expression to do this for you then just let us know.
    Salathe
    Software Developer and PHP Manual Author.


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
  •