Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jul 2011
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 Posts

    parse_url not working

    Hey everyone,

    I've used the following code on one one of my sites before to extract the video ID from a youtube URL:

    PHP Code:
    $url=$vidlink;
    parse_strparse_url($urlPHP_URL_QUERY), $my_array_of_vars);
    $vidcode=$my_array_of_vars['v']; 
    (Where $vidlink == the original url)

    And this works brilliantly. However, I'm trying to use the exact same code on another site and it's just not working. It won't output anything. Does anyone know of a reason this wouldn't work?

    Thanks

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    whats the url ? if its seo-friendly then there will not be a query string to grab e.g. mysite.com/index/name-of-page/page1 will fail with your function call

    if you show us the url we might be able to suggest some other method
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Regular Coder
    Join Date
    Jul 2011
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Any youtube URL, they generally follow the same standard format:
    As an example, here's an example:

    http://www.youtube.com/watch?v=_y9vz...feature=g-vrec

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    You didn't give us $my_array_of_vars so not sure what your looking for but maybe something like this:
    PHP Code:
    <?php
    $url 
    'http://www.youtube.com/watch?v=_y9vzYDaKRY&feature=g-vrec';
    $sam = (parse_url($url));
    echo 
    $sam["query"];
    ?>

  • #5
    Regular Coder
    Join Date
    Jul 2011
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sunfighter View Post
    You didn't give us $my_array_of_vars so not sure what your looking for but maybe something like this:
    $my_array_of_vars is the array created by the 'parse_str' function so there is surely nothing more to give on that?

  • #6
    Regular Coder
    Join Date
    Jul 2011
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 Posts
    In short I simply want to be able to take any url thats given and extract the variable with in it labled 'v'.

    So for example:
    "www.thisurl.com?v=ted"
    would output "ted"

    "www.thaturl.com?loc=bar&v=ball&steel=shed"
    would output "ball"

  • #7
    Regular Coder
    Join Date
    Jul 2011
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Found this. Bit more bulky but works like a dream:

    function getVariableFromUrl($url) { //we need to see if this URL is passing any GET variables

    $variablesStart = strpos($url, "?") + 1;

    if (!$variablesStart) {

    // no variables!

    return(false);

    }

    //before we extract the variables, we need to remove any anchors

    $variablesEnd = strpos($url,"#",$variablesStart);

    if ($variablesEnd) {

    $getVariables = substr($url, $variablesStart, $variablesEnd - $variablesStart);

    } else {

    $getVariables = substr($url, $variablesStart);

    }

    //next, we split the URL into an arrays containing variable name and value pairs (ie. "variable=value")

    $variableArray = explode("&", $getVariables);

    //we will iterate through each of the array pairs (ie. "variable=value")

    foreach ($variableArray as $arraySet) {

    $nameAndValue = explode("=", $arraySet);

    //using the above examples, $nameAndValue[0] would be "variable" and $nameAndValue[1] would be "value"

    $output[$nameAndValue[0]] = $nameAndValue[1];

    }

    return($output);

    }

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Erm, I'm not quite sure I follow your problem here. Your original post works fine for me:
    PHP Code:
    $url 'http://www.youtube.com/watch?v=_y9vzYDaKRY&feature=g-vrec';
    parse_str(parse_url($urlPHP_URL_QUERY), $a);
    if (isset(
    $a['v']))
    {
        
    printf('V has been supplied and it is: %s' PHP_EOL$a['v']);

    Code:
    V has been supplied and it is: _y9vzYDaKRY
    Unless I misunderstand completely what you are doing, then your original post should work as expected. I tried it with these other two links you have, and they also worked fine.
    What version of PHP are you using? Do you have more examples of something that's not working as expected (I do find that parse_url is somewhat picky at times, so this may be a version difference).


  •  

    Posting Permissions

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