Tiny little str_replace trouble.

Discussion in 'PHP' started by Nintendo, Feb 16, 2010.

  1. #1
    
    $v = str_replace($v, "", "http://www.youtube.com/watch?v=");
    $download_link = $tube->get('http://www.youtube.com/watch?v='.$_GET['v']);
    
    PHP:
    Is there any way to edit this code so that if you enter the full URL in a form, the part of the URL before the ID (http://www.youtube.com/watch?v=) get's deleted?

    The form is set up so you just enter for example 9lp0IWv8QZY for

    http://www.youtube.com/watch?v=9lp0IWv8QZY

    so I can have http://www.domain.com/index.php?v=9lp0IWv8QZY as a URL (to feed Google :D hince GET instead of POST), instead of a long URL with the full YouTube URL, and I'm trying to get it to also check for and get rid of the URL before the ID so the full URL can still be entered and work, and only have the ID part used.
     
    Nintendo, Feb 16, 2010 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    flip the $v and "http://www.youtube.com/watch?v=" and it will work.

    $v = str_replace( "http://www.youtube.com/watch?v=","",$v);
     
    shallowink, Feb 16, 2010 IP
  3. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #3
    koko5, Feb 17, 2010 IP
  4. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #4
    those functions wouldn't work if they entered just the video ID.
     
    shallowink, Feb 17, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    Regex is your friend:

    <?php
    
    $youtubelink = "http://www.youtube.com/watch?v=9lp0IWv8QZY";
    
    $youtubeid = preg_replace("/http:\/\/www\.youtube\.com\/watch\?v=([A-Z0-9]*)/", "$1", $youtubelink);
    
    echo $youtubeid;
    ?>
    PHP:
     
    danx10, Feb 17, 2010 IP
  6. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #6
    I would use the functions koko mentioned. If the input is a URL it will return the part you're looking for. If they enter just the ID then the functions will return an error and you'll know they just entered the ID.

    Are youtube IDs always the same length ?
    If they are you could use that as a queue. The return of those functions should be clue enough though. :)
     
    joebert, Feb 17, 2010 IP
  7. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #7
    I'd stick with the original. If its just the ID, there's no further code required. plus the http_build_url is PECL so its not always going to be available.
     
    shallowink, Feb 17, 2010 IP
  8. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #8
    For the life of me, the replace stuff wouldn't change the URL, so I went to the 301 mod_rewrite route.

    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php\?v=http\%3A\%2F\%2Fwww\.youtube\.com\%2Fwatch\%3Fv\%3D([^\ ]+)\ HTTP/
    RewriteRule ^index\.php$ http://www.domain.com/v/%1/? [R=301,L]

    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php\?v=([^\ ]+)\ HTTP/
    RewriteRule ^index\.php$ http://www.domain.com/v/%1/? [R=301,L]

    RewriteRule ^v/([^.]+)/$ index.php?v=$1 [L]

    covering both the full youtube URL and when just entering the video ID.
     
    Nintendo, Feb 17, 2010 IP
  9. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #9
    Ah ok. When you said "change the URL" you meant the URL that shows in their browsers address bar after they've submitted the form. I thought you were just worried about the input while the script was working with it.

    The server is probably doing less work with mod_rewrite than it would need to do to accomplish the same thing with PHP anyways. :D
     
    joebert, Feb 17, 2010 IP
  10. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #10
    Yep, I was trying to not have the URL after pressing submit be so long, with the full youtube URL of the video in there. Then I finally gave up on the search and replace in the script and went to the 301 redirect, which ended up being much better than I had originally wanted. It got rid of both versions of the dynamic URLs instead of just the long one

    domain.com/index.php?s=http://www.youtube.com/watch?v=XXXXXXX
    and
    domain.com/index.php?s=XXXXXXX

    making it so it's 100% static both ways, so that when you submit either the full youtube URL or just the video ID in the form, they both work and both redirect to domain.com/v/XXXXXXX/
     
    Nintendo, Feb 17, 2010 IP
  11. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #11
    In that case, check this out.

    If the user has javascript enabled this will correct the input before the form gets submitted, if they don't have JS enabled it will just work the way you have it now. In cases when they have JS enabled, this will save the server from needing to issue the 301 and handling a second request.

    I've included a whole test page here so you can see what it does (and test it because I'm on a Linux box right now and only have access to Firefox and Opera), but the part you want to pay attention to is the "onblur" bit of the text input.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    	<title>Title</title>
    </head>
    <body>
    	<div>
    	<form action="" method="get">
    		<input style="width:500px;" type="text" name="id" id="id" value="" onblur="this.value=/^http/i.test(this.value) ? this.value.replace(/^http.+v=([a-z\d]+)(&.+)?$/i, '$1') : this.value;"/>
    		<input type="submit" name="some_name" id="some_name" value="Submit"/>
    	</form>
    	</div>
    </body>
    </html>
    HTML:
     
    joebert, Feb 18, 2010 IP