Solved

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

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

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 74

Expert Comment

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

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

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
 

Author Closing Comment

by:morako
ID: 40008361
Perfect,  Thank you...  ;-)
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 34

Expert Comment

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

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 34

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 74

Expert Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now