1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to explode in 2 places.

Discussion in 'PHP' started by MrLeN, Aug 14, 2008.

  1. #1
    http://www.youtube.com/watch?v=BTPTrOfaN0U&feature=related

    How can I explode the parts in red?

    All I want is the video id.

    Also, what happens if there is no &feature=related ?

    Will adding code to remove it break?
     
    MrLeN, Aug 14, 2008 IP
  2. cianuro

    cianuro Peon

    Messages:
    1,857
    Likes Received:
    106
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Removing "&feature=related" will not effect it at all.
     
    cianuro, Aug 14, 2008 IP
  3. Joseph S

    Joseph S Well-Known Member

    Messages:
    1,373
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    155
    #3
    I can code a script for you that will extract the video ID. Drop me a PM.
     
    Joseph S, Aug 14, 2008 IP
  4. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #4
    This is what I have so far:

    
    <?php
    $str = "http://www.youtube.com/watch?v=BTPTrOfaN0U&feature=related";
    print_r (explode("v=",$str));
    print_r (explode("&",$str));
    ?> 		
    
    Code (markup):
    How can I remove the part before v= and the part after & ?

    Can I say like explode <= or explode =>

    How does that work?
     
    MrLeN, Aug 14, 2008 IP
  5. Kimochi

    Kimochi Peon

    Messages:
    31
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Kind of sloppy, but here you go:

    
    $str = 'http://www.youtube.com/watch?v=BTPTrOfaN0U&feature=related';
    $str_temp = explode('v=',$str);
    $str_temp = explode('&feature',$str_temp[1]);
    $str = $str_temp[0];
    
    Code (markup):
    Not tested.
     
    Kimochi, Aug 14, 2008 IP
  6. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #6
    I actually figured it out by searching on google for:

    "remove domain and string from a url"

    Then I found:

    http://www.astahost.com/index.php?showtopic=15518&view=findpost&p=102612

    Then I modified it for myself :D

    Then I wrote:
    
    <?php
    $original_url = "http://www.youtube.com/watch?v=BTPTrOfaN0U&feature=related";
    $split_url = explode('v=', $original_url);
    $split_url_again = explode('&', $split_url[1]);
    $youtube_video_id = $split_url_again[0];
    echo $youtube_video_id;
    ?> 		
    
    Code (markup):
    The code I made is very similar to yours -- but longer and sloppier - lol

    Thanks Kimochi
     
    MrLeN, Aug 14, 2008 IP
  7. Kimochi

    Kimochi Peon

    Messages:
    31
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    No problem. Please add to my reputation if I was at all helpful :)
     
    Kimochi, Aug 14, 2008 IP
    MrLeN likes this.
  8. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #8
    oh.. okies :)
     
    MrLeN, Aug 14, 2008 IP
  9. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #9
    I am looking everywhere for a reputation link -- where is it?
     
    MrLeN, Aug 14, 2008 IP
  10. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #10
    I found it! :D
     
    MrLeN, Aug 14, 2008 IP
  11. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #11
    I have one problem..

    I need to detect whether $original_url contains youtube.com -- as it is not coded into the site, $original_url actually comes from a form.

    How do I go about saying something like (and don't laugh at my explanation)...

    if ( url contains youtube.com ) {
    I put my regular code here
    }
    else {
    ENTER A YOUTUBE URL!
    }

    ?
     
    MrLeN, Aug 14, 2008 IP
  12. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #12
    Why PM? Are you gonna charge him for one line of code? :rolleyes:

    Anyway, forget these ugly explodes. This will do all in one...
    
    if (preg_match('~^http://(?:[a-z]{2,3}\.)?youtube\.com/watch\?v=([\w-]+)~', $original_url, $match))
    {
        echo $match[1];
    }
    else
    {
        echo 'Invalid URL.';
    }
    
    PHP:

    (On a side note, does your username have anything to do with Company Flow? :D)
     
    nico_swd, Aug 14, 2008 IP
    MrLeN likes this.
  13. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #13
    thanks nico_swd :)

    I kinda have to stick with what I made, because look - it's already such a mess lol

    
    if ($_POST['v01'] != '') { 
    $original_url = $_POST['v01'];
    $split_url = explode('v=', $original_url);
    $split_url_again = explode('&', $split_url[1]);
    $string = "".$split_url_again[0]."\n";
    }
    
    if ($_POST['v02'] != '') {
    $original_url_02 = $_POST['v02'];
    $split_url_02 = explode('v=', $original_url_02);
    $split_url_again_02 = explode('&', $split_url_02[1]);
    $string .= "".$split_url_again_02[0]."\n";
    }
    if ($_POST['v03'] != '') {
    $original_url_03 = $_POST['v03'];
    $split_url_03 = explode('v=', $original_url_03);
    $split_url_again_03 = explode('&', $split_url_03[1]);
    $string .= "".$split_url_again_03[0]."\n";
    }
    if ($_POST['v04'] != '') {
    $original_url_04 = $_POST['v04'];
    $split_url_04 = explode('v=', $original_url_04);
    $split_url_again_04 = explode('&', $split_url_04[1]);
    $string .= "".$split_url_again_04[0]."\n";
    }
    if ($_POST['v05'] != '') {
    $original_url_05 = $_POST['v05'];
    $split_url_05 = explode('v=', $original_url_05);
    $split_url_again_05 = explode('&', $split_url_05[1]);
    $string .= "".$split_url_again_05[0]."\n"; 
    }
    if ($_POST['v06'] != '') {
    $original_url_06 = $_POST['v06'];
    $split_url_06 = explode('v=', $original_url_06);
    $split_url_again_06 = explode('&', $split_url_06[1]);
    $string .= "".$split_url_again_06[0]."\n";
    }
    if ($_POST['v07'] != '') {
    $original_url_07 = $_POST['v07'];
    $split_url_07 = explode('v=', $original_url_07);
    $split_url_again_07 = explode('&', $split_url_07[1]);
    $string .= "".$split_url_again_07[0]."\n";
    }
    if ($_POST['v08'] != '') {
    $original_url_08 = $_POST['v08'];
    $split_url_08 = explode('v=', $original_url_08);
    $split_url_again_08 = explode('&', $split_url_08[1]);
    $string .= "".$split_url_again_08[0]."\n"; 
    }
    if ($_POST['v09'] != '') {
    $original_url_09 = $_POST['v09'];
    $split_url_09 = explode('v=', $original_url_09);
    $split_url_again_09 = explode('&', $split_url_09[1]);
    $string .= "".$split_url_again_09[0]."\n";
    }
    if ($_POST['v10'] != '') {
    $original_url_10 = $_POST['v10'];
    $split_url_10 = explode('v=', $original_url_10);
    $split_url_again_10 = explode('&', $split_url_10[1]);
    $string .= "".$split_url_again_10[0]."\n";
    }
    if ($_POST['v11'] != '') {
    $original_url_11 = $_POST['v11'];
    $split_url_11 = explode('v=', $original_url_11);
    $split_url_again_11 = explode('&', $split_url_11[1]);
    $string .= "".$split_url_again_11[0]."\n";
    }
    if ($_POST['v12'] != '') {
    $original_url_12 = $_POST['v12'];
    $split_url_12 = explode('v=', $original_url_12);
    $split_url_again_12 = explode('&', $split_url_12[1]);
    $string .= "".$split_url_again_12[0]."\n";
    }
    if ($_POST['v13'] != '') {
    $original_url_13 = $_POST['v13'];
    $split_url_13 = explode('v=', $original_url_13);
    $split_url_again_13 = explode('&', $split_url_13[1]);
    $string .= "".$split_url_again_13[0]."\n";
    }
    if ($_POST['v14'] != '') {
    $original_url_14 = $_POST['v14'];
    $split_url_14 = explode('v=', $original_url_14);
    $split_url_again_14 = explode('&', $split_url_14[1]);
    $string .= "".$split_url_again_14[0]."\n";
    }
    if ($_POST['v15'] != '') {
    $original_url_15 = $_POST['v15'];
    $split_url_15 = explode('v=', $original_url_15);
    $split_url_again_15 = explode('&', $split_url_15[1]);
    $string .= "".$split_url_again_15[0]."\n";
    }
    if ($_POST['v16'] != '') {
    $original_url_16 = $_POST['v16'];
    $split_url_16 = explode('v=', $original_url_16);
    $split_url_again_16 = explode('&', $split_url_16[1]);
    $string .= "".$split_url_again_16[0]."\n"; 
    }
    if ($_POST['v17'] != '') {
    $original_url_17 = $_POST['v17'];
    $split_url_17 = explode('v=', $original_url_17);
    $split_url_again_17 = explode('&', $split_url_17[1]);
    $string .= "".$split_url_again_17[0]."\n"; 
    }
    if ($_POST['v18'] != '') {
    $original_url_18 = $_POST['v18'];
    $split_url_18 = explode('v=', $original_url_18);
    $split_url_again_18 = explode('&', $split_url_18[1]);
    $string .= "".$split_url_again_18[0]."\n"; 
    }
    if ($_POST['v19'] != '') {
    $original_url_19 = $_POST['v19'];
    $split_url_19 = explode('v=', $original_url_19);
    $split_url_again_19 = explode('&', $split_url_19[1]);
    $string .= "".$split_url_again_19[0]."\n"; 
    }
    if ($_POST['v20'] != '') {
    $original_url_20 = $_POST['v20'];
    $split_url_20 = explode('v=', $original_url_20);
    $split_url_again_20 = explode('&', $split_url_20[1]);
    $string .= "".$split_url_again_20[0]."\n";
    }
    
    Code (markup):
    Yeah -- as you can tell, programming isn't my forte :D

    Nope, I am "the" MrLeN online. I own the dot com -- so there you go lol.
     
    MrLeN, Aug 14, 2008 IP
  14. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #14
    I know I could probably do some fancy loop or something, but I am lucky to be able creating what I am creating with what skills I have lol. I will post a link to the script I am making just as soon as I am a little bit more happy with it. It might be coded like a dogs breakfast (and I'm not even joking).. but it's a way cool script :D Does the job!
     
    MrLeN, Aug 14, 2008 IP
  15. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #15
    Oh my... haha.

    Use arrays!!! Name your input fields like this:
    
    <input type="text" name="v[]" />
    
    Code (markup):
    ... rather than the number things. And then use a loop in your PHP code:
    
    foreach ((array)$_POST['v'] AS $v)
    {
        echo $v;
        // Split $v or whatever you want to do with it.
    }
    
    PHP:
    ... that'd make it slightly shorter lol.
     
    nico_swd, Aug 14, 2008 IP
  16. ahowell

    ahowell Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #16
    wow..

    go with nico_swd's suggestion, and put it in a loop...

    $numFields    = 20;
    $originalUrls = array();
    
    for ($i = 0; $i < $numFields; $i++) {
        $field = "v{$i}";
        
        if (preg_match('~^http://(?:[a-z]{2,3}\.)?youtube\.com/watch\?v=([\w-]+)~', $_POST[$field], $match)) {
            $originalUrl[$field] = $_POST[$field];    
        } else {
            $originalUrl[$field] = 'EMPTY';
        }
    }
    PHP:
     
    ahowell, Aug 14, 2008 IP
  17. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #17
    Do I have to put something inside v[HERE] ?
     
    MrLeN, Aug 14, 2008 IP
  18. ahowell

    ahowell Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #18
    No, just put it as 'v[]'.

    <form>
        <input type='text' name='v[]' />
        <input type='text' name='v[]' />
        <input type='text' name='v[]' />
        <input type='text' name='v[]' />
        <input type='text' name='v[]' />
        <input type='text' name='v[]' />
        <input type='text' name='v[]' />
        <input type='text' name='v[]' />
        <input type='text' name='v[]' />
        <input type='text' name='v[]' />
    </form>
    PHP:
    Once the form posts, you will have this array.
    $_POST['v'][0] = 'some_url';
    $_POST['v'][1] = 'some_url';
    $_POST['v'][2] = 'some_url';
    $_POST['v'][3] = 'some_url';
    etc...
    
    PHP:

    $numFields    = 20;
    $originalUrls = array();
    
    for ($i = 0; $i < $numFields; $i++) {
        if (preg_match('~^http://(?:[a-z]{2,3}\.)?youtube\.com/watch\?v=([\w-]+)~', $_POST['v'][$i], $match)) {
            $originalUrl[$i] = $match[1];
        } else {
            $originalUrl[$i] = 'EMPTY';
        }
    }
    PHP:

    However, I would not do a foreach loop on $_POST['v'] as nico's code above does, as it is not very secure.
    A user could copy the form to their own server, and create thousands of fields, and post it back to your script which will get stuck parsing it all =)

    Specifying the expected field count (as in my reply above) will prevent that situation.
     
    ahowell, Aug 14, 2008 IP
  19. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #19
    okies, that does look a lot cleaner.. I'll have a play around and see if I can get it working that way.. I'll come back and let you know how I go. Thanks heaps for the explanation.

    I just registered the domain for this script right now :)

    I'll upload it and you can see what it does. It's pretty cool :)
     
    MrLeN, Aug 14, 2008 IP
  20. is0is0

    is0is0 Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    I personally would use regex if I were you, Nico already gave you a pattern for it, saves many lines of code and a headache if you have to look back on it.
     
    is0is0, Aug 14, 2008 IP