Need help building this class to get id from url using preg_match

building php class

Need help building this class to retrieve the id of the video.

the url is :  http://www.dailymotion.com/video/xsr67x

public function dailymotion($url, $w="400", $h="250") {
              if (preg_match('((http://|https://|www.)+(dailymotion.)[\w-\d]+(video)+(/)+(\d+))',$url, $id)){
              $id=$id[5];
                  return  $id ;
                }
      }

Please advise...
morakoAsked:
Who is Participating?
 
Dan CraciunConnect With a Mentor IT ConsultantCommented:
@kaufmed: I think the OP wants to make sure the link is from dailymotion.

This should match:
public function dailymotion($url, $w="400", $h="250") {
              if (preg_match('%(?:http://|https://)(?:www\.)?dailymotion\.com.*?video/(\w+)%',$url, $id)){
              $id=$id[1];
                  return  $id ;
                }
      }

Open in new window

HTH,
Dan
0
 
Ray PaseurCommented:
Given this for the input:

http://www.dailymotion.com/video/xsr67x

What do you want to get for the output?
0
 
käµfm³d 👽Commented:
Why not simply:

$id = substr($url, strrpos($url, "/"))
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
morakoAuthor Commented:
Perfect,  Thank you...  ;-)
0
 
Dan CraciunIT ConsultantCommented:
You're welcome. Glad I could help!
0
 
käµfm³d 👽Commented:
I think the OP wants to make sure the link is from dailymotion

I still think regex is overkill for that...

if (strpos($url, "dailymotion.com"))
{
    $id = substr($url, strrpos($url, "/"));
{

Open in new window

0
 
Dan CraciunIT ConsultantCommented:
You're probably right, but a regex will give a correct result on this:

http://www.dailymotion.com/video/xsr67x/

whereas strrpos will not.
0
 
käµfm³d 👽Commented:
I'll spare you the simple change that would account for that variation   ; )
0
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.

All Courses

From novice to tech pro — start learning today.