Help me write some RegEx to use in Preg_Match - pull Wistia Video ID from URL

Hi there,
I haven't ever got my head around RegEx sorry - and this is a once off.

Can someone give me the preg_match RegEx required to pull the "ID" from this URL
http://auspicious.wistia.com/medias/ze56j8drs1?embedType=api&videoFoam=true&videoWidth=640

The id is ze56j8drs1
The URL could be
http(s)://*wistia.com/medias/*
http(s)://*wistia.com/embed/*
http(s)://*wi.st/medias/*
http(s)://*wi.st/embed/*

The guide on this page didn't help http://wistia.com/doc/construct-an-embed-code
Nor did "RegEx Buddy"

Thanks so much in advance RegEx experts :) I am sure this is simple as :)
LVL 1
Craig LambieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan CraciunIT ConsultantCommented:
$subject = "http://auspicious.wistia.com/medias/ze56j8drs1?embedType=api&videoFoam=true&videoWidth=640";
if (preg_match('%https*://.*(?:wistia\.com|wi\.st)/(?:medias|embed)/(.*?)\?%', $subject, $regs)) {
	$result = $regs[1];
} else {
	$result = "";
}
echo $result;

Open in new window


I wouldn't assume something is easy just because I don't know how to do it...

HTH,
Dan
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
The rules you choose for selection patterns are the "interesting" part of this question, and using regex when you don't really understand regex (or even when you do) is risky.  Dan's solution probably works - his are always exactly right - but I would ask you to think through the selection patterns carefully.  As I look at it, I see a RESTful URL with the target string in the rightmost end of the path.  So I might write something like this:
http://iconoun.com/demo/temp_cclambie.php
<?php // demo/temp_cclambie.php

/**
 * http://www.experts-exchange.com/Programming/Languages/Regular_Expressions/Q_28665324.html
 */
error_reporting(E_ALL);

// TEST DATA FROM THE QUESTION
$url = 'http://auspicious.wistia.com/medias/ze56j8drs1?embedType=api&videoFoam=true&videoWidth=640';

// A FUNCTION TO ISOLATE THE TAIL OF THE URL PATH
function get_wistia_video_id($path)
{
    $arr = explode(DIRECTORY_SEPARATOR, $path);
    $vid = end($arr);
    $arr = explode('?', $vid);
    $vid = current($arr);
    return $vid;
}

// SHOW THE WORK PRODUCT
$vid = get_wistia_video_id($url);
echo PHP_EOL . $url;
echo PHP_EOL . $vid;

Open in new window

In my experience, these kinds of  variable data strings are easier to understand if you have a test-driven development pattern that can account for the edge cases as well as the mainstream cases.  Example here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html
0
Craig LambieAuthor Commented:
Fantastic response !

Thanks for the tip Dan - I will take that aboard - although I think RegEx seems so simple if I only took the time to understand it (the very odd occasion I need it)

Ray - Noted, and appreciate your reply, but will use the simple version from Dan for now - if it breaks, it breaks.
I will keep this A in case and reference it in the Wordpress Plugin I am developing for this.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.