0

i have url: youtube.com/v/NRH2jEyiiLo&hl=en&fs=1&rel=0

And i need only code: NRH2jEyiiLo&hl.

preg_match($pattern, $url, $matches);

I use this pattern: $pattern = '@^([^/v/]+)?([^=]+)@i';

But why result is: "youtube.com/v/NRH2jEyiiLo"?

How drop "youtube.com/v/", thanks ;)

1
  • 2
    Do you have only that URL, or is it embedded in a larger block of other content? Commented Dec 24, 2010 at 15:27

3 Answers 3

1
  $pattern = '@^([^/v/]+)?([^=]+)@i';

using this pattern then

$str= "youtube.com/v/NRH2jEyiiLo";

$newarr = explode('/',$str);

echo $newarr[2];
Sign up to request clarification or add additional context in comments.

5 Comments

This will work if nothing is beyond video code, thus not very reliable. What if someone inputs this code youtube.com/watch?v=9beQh1yH5uU&feature=related ?
I give the answer after pattern matching
That will work in this case, but not in many others ;) youtube.com/user/AlJazeeraEnglish#p/u/0/3ZrQYJppX1A , you never know what will user input.
But is asking for How drop "youtube.com/v/",
No, he is asking how to extract video code, but your code also works.
1

Here's mine... (updated)

$url = "youtube.com/v/NRH2jEyiiLo&hl=en&fs=1&rel=0";
preg_match("#[A-Za-z0-9\-\_]{11}#", $url, $video);
echo($video[0]); //NRH2jEyiiLo

If you want to embed videos in your webpage, you can try this function... http://webarto.com/57/php-youtube-embed-function

Comments

0

You shouldn't use a character class there, nor the start anchor.

$pattern = '@/v/(.*?)&@';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.