GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   regex nightmare: anyone want to check it? (https://gfy.com/showthread.php?t=927806)

fris 09-15-2009 08:35 AM

regex nightmare: anyone want to check it?
 
My youtube wordpress plugin works via

[!youtube]L1BDM1oBRJ8[/youtube!]

I also wanted to be able to do it via

https://youtube.com/watch?v=L1BDM1oBRJ8
https://youtube.com/watch?v=L1BDM1oBRJ8
www.youtube.com/watch?v=L1BDM1oBRJ8
youtube.com/watch?v=L1BDM1oBRJ8
watch?v=L1BDM1oBRJ8
v=L1BDM1oBRJ8
L1BDM1oBRJ8
https://youtube.com/v/L1BDM1oBRJ8

Code:

$regex = "/\[(?:(?:http:\/\/)?(?:www\.)?youtube\.com\/)?(?:(?:watch\?)?v=|v\/)?([a-zA-Z0-9\-\_]{11})(?:&[a-zA-Z0-9\-\_]+=[a-zA-Z0-9\-\_]+)*\]/";

borked 09-15-2009 08:41 AM

well, the simplest is to optionally allow each component:

(http:\/\/)?(www\.)?(youtube\.com)?(\/)?(watch\?)?(v=)?([a-zA-Z0-9]{11})

but then this means that any optional component can be left out eg:
http://v=L1BDM1oBRJ8

is valid.

BestXXXPorn 09-15-2009 08:42 AM

That should work, it might be a little overkill with all the valid character checking though :P

You could also:

$arr = explode('=', $youTubeURL);

and validate just the portion you want:

$linkCode = $arr[count($arr)-1];

While that wouldn't work with the last case you posted you could just do a:

$youTubeUrl = str_replace('/', '=', $youTubeUrl);

before hand since you don't care what the domain is anyway... It would definitely be much faster than using regex, parsing wise :P

BestXXXPorn 09-15-2009 08:51 AM

Running benchmarks on regex (using borked's simplified regex) vs explode (since we're trying to strip the actual value for v...

I show explode method as twice as fast as regex ;)


All times are GMT -7. The time now is 12:07 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123