lolalola
lolalola

Reputation: 3823

php preg_match and pattern

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 ;)

Upvotes: 0

Views: 344

Answers (3)

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

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

Upvotes: 1

Pradeep Singh
Pradeep Singh

Reputation: 3634

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

using this pattern then

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

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

echo $newarr[2];

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 838256

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

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

Upvotes: 0

Related Questions