-4

Possible Duplicate:
How do I get the Video Id from the URL? (DailyMotion)

I have this code for getting dailymotion thumbnail of the video:

<?php
    $url = get_the_content(); 
    if (preg_match('????????????', $url, $match)) {
        $video_id_dailymotion = $match[1];
    }
?>

<?php if($video_id_dailymotion): ?>
    <img class="video-thumbnail" src="http://dailymotion.com/thumbnail/video/<?php echo $video_id_dailymotion; ?>" alt="" width="190">
<?php endif; ?>

But need help with the preg_match function part. What should I put instead of ???????????? as the first argument?

The format of dailymotion video is e.g.:

http://www.dailymotion.com/video/xulmeo_osama-bin-laden-for-dummies_shortfilms

Btw. the $url variable contains other stuff like youtube object code, a lot of text etc. and only sometimes embeded dailymotion video.

EDIT: for youtube I was using something like this:

if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match)) {
    $video_id_youtube = $match[1];
}

And it was working, now I need something similar for dailymotion.

EDIT 2: It has to be done using preg_match and use dailymotion.com in that regex condition

EDIT 3: The code in $url that need to be processed by preg_match can look like this e.g.:

And here is the video <iframe src="http://www.dailymotion.com/embed/video/xsqxky" frameborder="0" width="480" height="270"></iframe>
What do you think? Let us know. Peace!
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Derfder
  • 3,204
  • 11
  • 50
  • 85
  • Well, it does not looks like that you are that far off. And I'm pretty sure that you are clever enough to know that the youtube regex does not work. The more interesting part would be to know into which concrete problem you run with the regex. Where do you hit the roadblock? Otherwise asking for a regex is just like asking for code, but we like to add some value and want to answer questions, not just provide code. – hakre Nov 01 '12 at 12:51
  • Actually the youtube regex works my friend. – Derfder Nov 01 '12 at 13:04
  • I will probably start a bounty for this question in 2 days, so please, if you just want to throw your opinions, I ask you to stop and let others WHO MIGHT WANT TO actualy HELP me answer my question. Thank you. – Derfder Nov 01 '12 at 13:06

2 Answers2

4

For dailymotion the video ID is the first string(before the first _) of the 3rd URL segment. As per your example

www.dailymotion.com/video/xulmeo_osama-bin-laden-for-dummies_shortfilms

has the video id is xulmeo. So all you need to do is extract the string before the first occurrence of underscore

Code :

$slice=explode('/',$url);
$video_id_dailymotion=explode('_',$slice[2]);

or simply

$video_id_dailymotion=strtok(basename($url), '_');

Refer :
explode
strtok
basename


Edit: As pointed out, there may are different ways in which dailymotion links appear.

  1. http://www.dailymotion.com/embed/video/xulmeo
  2. http://www.dailymotion.com/video/xulmeo_osama-bin-laden-for-dummies_shortfilms

For case 2 refer the previous snippets and for both cases use this code

$slice=explode('/',$url);
$video_id_dailymotion=(strlen($slice[2])==6?$slice[2]:explode('_',$slice[2]));
// All dailymotion video id's are 6 in length
  • No, as I have mentioned it in my question already, I have many links (not only dailymotion) in $url variable, so therefore it can go wrong when uisng your code. It has to be done using preg_match and use dailymotion.com in that regex. – Derfder Nov 01 '12 at 12:35
  • I have added more clarification to my question to make even clearer. If anything is missing, let me know and I will add the additional information in my question. Thanks. – Derfder Nov 01 '12 at 12:47
  • The code I gave is specific only for dailymotion and will work only for dailymotion. For other sites you need to re-write depending on the link. So determine what website link you have (dailymotion/youtube/etc) and then use this code for dailymotion –  Nov 01 '12 at 12:50
  • @Derfder: What is *wrong* with putting that code into a function of it's own and use that function then instead of `preg_match`? I don't see any kind of difference to run that equally programmatically. No need to downvote IMHO. Also Sai does already big part of the string analysis here which is equally useful for a regular expression based solution. However nobody here *needs* to write a regex for you, you just have asked how it is done. – hakre Nov 01 '12 at 12:53
  • You don't understand it can be http://www.dailymotion.com/embed/video/xsqxky or http://www.dailymotion.com/video/xulmeo_osama-bin-laden-for-dummies_shortfilms so the format is different. I need to find out all dailymotion.com links in the code, whether it is within src="" or href="" and then parse it using some regex inside preg_match to catch everything before the last slash. – Derfder Nov 01 '12 at 12:54
  • @Derfder: Then provide a list of all dailymotion links you could find so far. Do that work *before* asking the question. That will greatly improve your question. Improved questions do get improved answers, that simple it is. – hakre Nov 01 '12 at 12:55
  • @hakre I need it using preg_match because it is the simpliest solution possible. If you know how to write such a regex feel free to write it down, otherwise please respect my conditions. Thanks in advance. – Derfder Nov 01 '12 at 12:56
  • @hakre for now just these 2 possibilities src="http://www.dailymotion.com...." or href="http://www.dailymotion.com...." – Derfder Nov 01 '12 at 12:57
  • Please don't feel offended. As written this site is not about asking for code but a programming question. String pattern analysis is part of your issue and Sai answered that. If you see the many edits you have in your question, it is obvious that you have first of all a problem / hard time to fully describe your issue. Please equally resepect, that the helpful users here even have a harder time to understand your issue because it's not their issue. Please respect that. – hakre Nov 01 '12 at 12:58
  • I don't think Sai answered my question at all. His code is wrong. He was not considering $url content (which is a lot of code , text etc.). Also, he didn't use preg_match function and regex. he instead that used explode function. i was not asking to use explode. – Derfder Nov 01 '12 at 13:01
  • @Derfder I updated the code as per the two links you gave me and this may not be ur answer but its one of the solution to your question which may not be best but it solves the case in hand. Working on `preg_match` will update as soon as I get it –  Nov 01 '12 at 13:06
  • OK, I appreciate your effort. I have upvoted your answer and looking forward for your preg_match answer. Please, take in mind that $url variable contains basically the whole article from wordpress via get_the_content() function so there could be 1 link from youtube, 2 from dailymotion within that $url variable besides other html tags and a text of course. I just need the first dailymotion link to be used that this function find. – Derfder Nov 01 '12 at 13:10
  • I agree with @hakre this isn't a asking for code forum we discuss different solutions here. When I first answered ur question u didn't mention that u want to use preg_match explicitly. Then later you made many edits to the question. The way you asked question are clearly reflected in the downvotes.(No Offense Intended) –  Nov 01 '12 at 13:13
  • This is what you need to mention clearly in the question that the url will contain several links. Extract all the links from the page, determine its domain (youtube/wordpress) , call the respective function. –  Nov 01 '12 at 13:16
  • What about something like this: #http://www.dailymotion.com/embed/video/([A-Za-z0-9]+)#s or some better code instead of giving excuses? Think about it Sai. Btw. I have made 3 edits to make my question more clear. So, consider reading my question once more with all the edits I have made so far and ask me what is missing and not clear and I will add it there. – Derfder Nov 01 '12 at 13:23
  • @Derfder: Nobody is giving excuses here. Please treat other users on site with respect. Thank you in advance. – hakre Nov 01 '12 at 13:35
0

First of all use a HTML parser to extract all links. That would enable you to have the information in a prefered form.

Then normalize all links according to the underlying specification, namely RFC3986 Section 6.

Now all URLs are in a format you can use for comparison and analysis. For example with a regular expression.

For these three steps, tools and answers exist:

  1. DOMDocument and DOMXPath - see How to parse and process HTML with PHP?
  2. Net_Url2 - see XPath Query & HTML - Find Specific HREF's Within Anchor Tags
  3. preg_match - See How do I get the Video Id from the URL? (DailyMotion)

It is normally expected that you do your homework before asking a question, but I leave this generalized procedure here for you and future visitors that are "just having problems" and don't see the wood for the trees.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • 1
    Its clearly visible how @derfder conveniently diverted the question he asked initially. He initially asked "How to get Dailymotion code.." and then restricted to preg_match and then extracting dailymotion id from the whole article of wordpress containing different links and text. –  Nov 01 '12 at 13:25
  • 1
    @derfder here is your solution. No one can code the whole thing for you. Its all about getting suggestions when you go wrong. –  Nov 01 '12 at 13:26
  • @SaiSasidhar: Yes I'm totally with you. – hakre Nov 01 '12 at 13:36
  • I was asking for this: preg_match('#http://www.youtube.com/embed/([A-Za-z0-9]+)#s', $url, $match) or preg_match('#http://www.dailymotion.com/embed/video/([A-Za-z0-9]+)#s', $url, $match) . But in a more clever and hackerish way. If you consider a regex a "the whole thing for you" I suggest you to "rethink" your logic. Thanks. – Derfder Nov 01 '12 at 13:45
  • both is taken care of in the linked, duplicate question. Which also is part of my answer (in form of a link). But probably you can just downvote and moan and that is just all. Very unfriendly of you. – hakre Nov 01 '12 at 13:46
  • English is not my first language, so editing multiple times an article is for me "casual". I think that editing question is better than not-editing at all. Anyway, I don't have time to argue about interpretation. It's all just point of interpretation. So, this is my last comment to this question. Have a nice day. – Derfder Nov 01 '12 at 13:48
  • Well, what should I say? If you do reserve the right for you to edit the question, I think it should be more than understandable that users answering an earlier version of it might not have the latest version answered out of nothing, right? So just respect users if they are only willing to answer your first version of the question. There is nothing wrong with that. – hakre Nov 01 '12 at 13:59
  • Brace ur self up. Put this question to bounty only then I'm gonna ANSWER (that again depends) `preg_match` regex for dailymotion that works in all cases. Also with cases where link is anywhere in between words special characters multiple lines –  Nov 01 '12 at 14:17
  • I suggest you tidy up your question then. It might get you better results then. – hakre Nov 01 '12 at 14:19