1

Im trying to write plugin that will catch video details from various video websites simply by copy and paste url from browser.

But im having problems with metacafe videos. To fetch details from example this video url

http://www.metacafe.com/watch/cb-V9EJSvnKvTcH/confronting_fear_in_virtual_reality/

I need to go to metacafe http://www.metacafe.com/api/item/cb-V9EJSvnKvTcH/ and parse xml from this video id.

I thought it would be easy but problem is metacafe use only video id for xml data and url contains also video name after id.

To embed only video i use

$video_grab_url = 'http://www.metacafe.com/watch/cb-V9EJSvnKvTcH/confronting_fear_in_virtual_reality/';
$embed_vid_url = parse_url( $video_grab_url );
$metacafe = mb_substr( $embed_vid_url['path'], 7, -1 );

With this i get video id with name for embed. But as i said for other details i need to parse xml data from url and to get url of xml data i need only video id without name.

Im not php pro so i got little lost in using preg_match How do i use preg_match to get only this "cb-V9EJSvnKvTcH" so i can pull xml data and parse info like duration, thumb, tags, etc...

2 Answers 2

1

Try this one. Maybe this can help you.

(?<=watch/).*?(?=/)

sample PHP code,

<?php
$subject = "theLINKhere";
$pattern = "#(?<=watch/).*?(?=/)#"; // edit: Added modifiers
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>
4
  • And on this i get error like : preg_match() [function.preg-match]: Unknown modifier '.' Although this looks very similar as solution i found for vimeo video, but im not so familiar how to create pattern
    – lonerunner
    Sep 30, 2012 at 15:51
  • I came back to say that is missing something to pattern and see edited post, and now it works. Perfect. Thanks. Now if only someone can explain me what all these things in pattern means, or point me somewhere where i can learn more on how to format pattern for future usage.
    – lonerunner
    Sep 30, 2012 at 16:34
  • @AleksandarĐorđević this is the best site (as for me) for learning Regular Expression CLICK HERE
    – John Woo
    Sep 30, 2012 at 16:35
0

Or you can also use something like this:

$url = 'http://www.metacafe.com/watch/cb-V9EJSvnKvTcH/confronting_fear_in_virtual_reality/';
$path = parse_url($url, PHP_URL_PATH);
$pieces = explode('/', $path);

$video_id = $pieces[2];
1
  • Fixed now - I write ',' instead of '/' as explode delimiter.
    – crash01
    Sep 30, 2012 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.