Solved

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

Posted on 2014-04-17
8
402 Views
Last Modified: 2014-04-18
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...
0
Comment
Question by:morako
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40008011
Given this for the input:

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

What do you want to get for the output?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40008079
Why not simply:

$id = substr($url, strrpos($url, "/"))
0
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 40008236
@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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:morako
ID: 40008361
Perfect,  Thank you...  ;-)
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40008367
You're welcome. Glad I could help!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40008583
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
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40008593
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40008652
I'll spare you the simple change that would account for that variation   ; )
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question