regex nightmare: anyone want to check it?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fris
    Too lazy to set a custom title
    • Aug 2002
    • 55700

    #1

    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

    http://www.youtube.com/watch?v=L1BDM1oBRJ8
    http://youtube.com/watch?v=L1BDM1oBRJ8
    www.youtube.com/watch?v=L1BDM1oBRJ8
    youtube.com/watch?v=L1BDM1oBRJ8
    watch?v=L1BDM1oBRJ8
    v=L1BDM1oBRJ8
    L1BDM1oBRJ8
    http://www.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\-\_]+)*\]/";
    Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence.
  • borked
    Totally Borked
    • Feb 2005
    • 6284

    #2
    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.

    For coding work - hit me up on andy // borkedcoder // com
    (consider figuring out the email as test #1)



    All models are wrong, but some are useful. George E.P. Box. p202

    Comment

    • BestXXXPorn
      Confirmed User
      • Jun 2009
      • 2277

      #3
      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
      ICQ: 258-202-811 | Email: eric{at}bestxxxporn.com

      Comment

      • BestXXXPorn
        Confirmed User
        • Jun 2009
        • 2277

        #4
        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 ;)
        ICQ: 258-202-811 | Email: eric{at}bestxxxporn.com

        Comment

        Working...