Solved

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

Posted on 2014-04-17
8
399 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
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Ajax and PHP 4 47
Php logic to add to date card 9 37
pass foreach data in php into jquery ajax 10 44
assigning javascript variable to php variable 8 32
These days socially coordinated efforts have turned into a critical requirement for enterprises.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

685 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