Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Need help writing a regex to convert a YouTube Embed URL.

Last post 04-16-2008, 7:43 PM by Aussie Susan. 3 replies.
Sort Posts: Previous Next
  •  04-15-2008, 12:27 PM 41392

    Need help writing a regex to convert a YouTube Embed URL.

    Hello, I'm modifying some code that scans through an html document, and converts YouTube links to a URL of our server with $replacement1+ the YouTube ID as a URL parameter "url" (which you can see in the last line).  Everything seems ok, I just can't figure out the line for $pattern1.  I've tried probably 100 things with no luck.  I've left it here as ??????????? so it shows up well.  As you can see the embed src shows up in the $2 string.  The language I'm using is PHP. 

    An example YouTube Embed URL is:

    http://www.youtube.com/v/i4dLGAx2Q_s&hl=en

    ...and if everything works right it should convert to... 

    http://video.-------------.com/wsw?ct=Video&url=http://youtube.com/watch?v=i4dLGAx2Q_s   

    ...where i4dLGAx2Q_s is the piece I need to send as that URL parameter in the last line below. (It appears YouTube IDs can be alphanumeric + underscores and possibly dashes.)

    Thank you so much. I've been kicking this around for two days and I've had no luck. :( -Jason

     ......

      ......

            if(!$this->embed) // handle videos
            {
               
                //images without alt
                $search[]  = "/<embed[^>]* src=(\"([^\"]+)\"|'([^']+)'|([^\"'> ]+))[^>]*>/ie";
                       
                            $s1 = "$2";
                            $pattern1 = "???????????";
                            $replacement1= "http://www.youtube.com/watch?v=\\2";
                            $newurl = ereg_replace($pattern1, $replacement1, $s1);
                           
                $replace[] = "'[<a href=\'http://video.-------------.com/wsw?ct=Video&url='.'$newurl'.'\' />Watch Video</a>]'";
            }

    ......

    ......
     

    Filed under: , , , , ,
  •  04-15-2008, 7:11 PM 41418 in reply to 41392

    Re: Need help writing a regex to convert a YouTube Embed URL.

    I don't know enough about PHP to interpret your code or to tell you how to use this, but using a pattern of:

    http://www\.youtube\.com/v/([^&]*)&hl=en

    and a replacement string of:

    http://video.-------------.com/wsw?ct=Video&url=http://youtube.com/watch?v=$1

    (with the 'ignore case' option set) converts:

    http://www.youtube.com/v/i4dLGAx2Q_s&hl=en

    into:

    http://video.-------------.com/wsw?ct=Video&url=http://youtube.com/watch?v=i4dLGAx2Q_s

    which is what I think you are after. You may need to replace the backreference ('$1' in my case) with something else that suits the regex syntax you are using ('\1'???). Also, you can extend or reduce the 'literal' text in the pattern, and the replacement string, but remember that the whole of the replacement string will replace all of the text that the pattern matches.

    Susan

     

  •  04-16-2008, 1:11 PM 41448 in reply to 41418

    Re: Need help writing a regex to convert a YouTube Embed URL.

    Aussie Susan pattern looks perfect just an additional comment about php. I would advice you to use preg instead of ereg. Ereg is usally slower then pref, will not be included in future versions of PHP and you have much more options in preg. Just remember you need to escape the / char.

    preg_match('/http:\/\/www\.youtube\.com\/v\/([^&]*)&hl=en/i', 'http://www.youtube.com/v/i4dLGAx2Q_s&hl=en', $result);

    Even better would be to just replace the value right away.

    preg_replace('/http:\/\/www\.youtube\.com\/v\/([^&]*)&hl=en/i', '[<a href=\'http://video.-------------.com/wsw?ct=Video&url=$1\' />Watch Video</a>]', 'http://www.youtube.com/v/i4dLGAx2Q_s&hl=en');

    will result in

    [<a href=\'http://video.-------------.com/wsw?ct=Video&url=i4dLGAx2Q_s\' />Watch Video</a>]

    Hope that helps.

    Check out my regex tool at regex.larsolavtorvik.com
  •  04-16-2008, 7:43 PM 41457 in reply to 41448

    Re: Need help writing a regex to convert a YouTube Embed URL.

    aCa,

    Your comment triggered something in the back of my mind - I think ereg uses POSIX while preg uses a more expanded set of operators.

    This also has implications in some matching situations because POSIX is implemented as a DFA which means that it finds the longest match and not necessarily the first match. Depending on the situation, creating the DFA can take some time, but it can be very fast to match as it never backtracks. However, using an NFA-basde engine which means it has backtracking can allow the use of other techniques that are not possible in a DFA.

    Susan 

View as RSS news feed in XML