0

This is the url :

http://www.youtube.com/watch?src_vid=1OO30GoDSxg&feature=iv&annotation_id=annotation_787245&v=lsTEFZXJy28

Currently i'm using this code to get the ID

$url = $video.'&';
$pattern = '/v=(.+?)&+/';
preg_match($pattern, $url, $matches);
echo $matches[1];

And is working if i test it without the html POST form, because its look like the html form change the link to http://www.youtube.com/watch?src_vid=1OO30GoDSxg

//Edit

I have a simple html form and when the submit button is clicked I send the data with ajax to php page. If i echo $_POST['video']; i get http://www.youtube.com/watch?src_vid=1OO30GoDSxg but url that was insert is http://www.youtube.com/watch?src_vid=1OO30GoDSxg&feature=iv&annotation_id=annotation_787245&v=lsTEFZXJy28 .. soo why that is happening

Ben
  • 1,906
  • 10
  • 31
  • 47
  • Please use the search, there are so many code-examples already available how to get it. – hakre Apr 25 '12 at 08:54
  • I get the video ID but the html FORM change the hole url .. thats the problem – Ben Apr 25 '12 at 08:54
  • Which form? I don't see anything next to the word "form" that actually represents something "form" in your question. – hakre Apr 25 '12 at 08:55
  • possible duplicate of [regex pattern to get Youtube ID from any Youtube URL](http://stackoverflow.com/questions/9594943/regex-pattern-to-get-youtube-id-from-any-youtube-url) – Aleks G Apr 25 '12 at 08:58
  • I edited the question guys :) and is not about the video ID is about why the url is shorten – Ben Apr 25 '12 at 09:20
  • possible duplicate of [parse youtube video id using preg\_match](http://stackoverflow.com/questions/2936467/parse-youtube-video-id-using-preg-match) – Peter O. Oct 21 '12 at 02:19

1 Answers1

2
$parts = parse_url($url);
parse_str($parts['query'], $query);
echo $query['src_vid'];

parse_url()
parse_str()

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • I have a filter return preg_replace('/[^^א-תÀ-ÿ一-龥а-яa-zA-Z0-9#@.?=:_()\/\-" ]/','',$video); and the & is not selected but if i add it it dosent do anything thats why is changing the link – Ben Apr 25 '12 at 09:00
  • If all you want to do is extract the video ID from the URL, the above code is all you need. The two functions above should gracefully handle any odd characters, all you need is the `src_vid` key from the query string. Or are trying to apply some transformation to the url yourself? If so, what? – DaveRandom Apr 25 '12 at 09:03
  • I have a html form for new posts and the user can add a video url. When submit i filter the data and remove any symbols that arent allowed. The thing is that for the video url i remove the filter just the post data and still the link is shorten then the original. Somehow the html form change it because it contain **&**. If the video url is something like so www.youtube.com/watch?v=fWNaR-rxAic iI get the video ID but as you can see the url dont contain **&** – Ben Apr 25 '12 at 09:08
  • please show your HTML form, the browser should not perform any transformations unless you tell it to. – DaveRandom Apr 25 '12 at 09:20
  • and then i use $("#video").val() and simply send the data with ajax. Maybe the js is doing something .. – Ben Apr 25 '12 at 09:22
  • What do you get if you `var_dump()` the URL at the top of the PHP script? – DaveRandom Apr 25 '12 at 09:32
  • You need to pass the string you get from `$("#video").val()` through `encodeURIComponent()` in javascript before you send it to the server – DaveRandom Apr 25 '12 at 09:36
  • Yes that do the trick.. :) thanks alot Dave ^_^ I will go and read more about **encodeURIComponent()** – Ben Apr 25 '12 at 09:39