Jack Brown
Jack Brown

Reputation: 580

Strip Youtube Link from URL

I own a website which allows users to download Youtube videos. I need php scripts which detects if a youtube url is present, like:

givemeyoutube.com takes users to my normal website but if they type,

www.givemeyoutube.com/watch?v=OZLUa8JUR18

how do I take them to the index of my website and strip out the "watch?v=OZLUa8JUR18" and store it as a variable?

UPDATE: How can I do it so that it checks URL and if it contains "watch?v=" then redirect to another page?

Upvotes: 1

Views: 499

Answers (4)

jeroen
jeroen

Reputation: 91734

I think url rewriting would be better suited for this task, as you need to rewrite the url anyway to make sure that watch opens an actual php page.

I would use something like:

RewriteEngine On
RewriteBase /

RewriteRule ^watch.*$ /index.php?action=watch [QSA]

This rule would cause all requests that start with watch to be routed to index.php with the original query string (due to [QSA]) and an additional parameter called action with the value watch.

Upvotes: 1

Varol
Varol

Reputation: 1858

You can to use mod_rewrite to properly take care of a request like watch?V=123456

in your .htaccess file:

RewriteEngine On
RewriteBase /
RewriteRule ^watch index.php [QSA]

then in index.php:

if( isset( $_GET['v'] ) ) {
    // access the v parameter with $_GET['v'];
}

Upvotes: 1

ilanco
ilanco

Reputation: 9957

Use preg_match with the following regular expression

/(\/watch\?v=)([a-zA-Z0-9_-]+)/

Upvotes: 2

Moritz
Moritz

Reputation: 153

Preg match with \b(?<=v.|/)[a-zA-Z0-9_-]{11,}\b as RegExp

Upvotes: 1

Related Questions