Solved

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

Posted on 2014-04-17
8
400 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
Industry Leaders: 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
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 …

752 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