How to explode in 2 places.

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

  1. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #21
    I have just uplaoded a beta version of the site:

    http://www.tubeshine.com

    Tell me what you think.

    I REALLY need to find a way to validate the fields so that if they don't contain youtube.com then the form wont submit. I am also thinking about giving each IP a session and if they try to submit too many times it will just deny them -- and some other things. Maybe capcha, because there's no registration or emails required on this site. People can just come along and add all the lists they want.

    It wont hurt none if they add stupid lists or error code, because it doesn't show on the site. But I'd like to try and keep the server clean if possible.

    I am going to go and implement that code above on my localhost now.. see if I can get it working.

    How to I go about this?

    if ( url contains youtube.com ) {
    I put my regular code here
    }
    else {
    ENTER A YOUTUBE URL!
    }
     
    MrLeN, Aug 14, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #22
    nico_swd, Aug 15, 2008 IP
  3. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #23
    Sorry nico_swd, the code is so foreign to me, I didn't realise that you gave me the answer. When I looked at that code I thought it was only to get rid of the explodes. I feel la bit dim now :/

    In my defense, I was very tired. I have had a sleep now and I am getting back to implementing all the code that was suggested. One of the things I have to do is try to figure out "why" the code does what it does so I can remember for the future. I have a real hard time remembering a lot of programming, and even the programming I "can" do took me a very long time to learn and remember. But I am getting better! ..slowly, lol
     
    MrLeN, Aug 15, 2008 IP
  4. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #24
    I am trying to implement this code now, but it is not being placed in the text file. Originally I had like:

    
    $listf = fopen ($_SERVER['DOCUMENT_ROOT'] . '/videos/'.$_POST['new_video'].'.txt', 'w+');
    
    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";
    }
    
    etc etc etc
    
    fwrite($listf,$string);
    fclose($listf);
    header('Location: /code.php?v='.$_POST['new_video'].'');
    
    Code (markup):
    So I tried:

    
    $listf = fopen ($_SERVER['DOCUMENT_ROOT'] . '/videos/'.$_POST['new_video'].'.txt', 'w+');
    
    foreach ((array)$_POST['v'] AS $v)
    {
        #echo $v;
    		$string = "".$v."\n";
    }
    
    fwrite($listf,$string);
    fclose($listf);
    header('Location: /code.php?v='.$_POST['new_video'].'');
    
    Code (markup):
    It created the text file, but it didn't put anything in it.

    The old way I had it, basically it just gets all the youtube ID's out of the form and posts them one after the other like this:

    
    XLh4-XTnXq8
    5z0F0CzMPrE
    jwsHrst8Grw
    mCLmFtu_O1Q
    hPRcJE6k1io
    -m19mzFObvU
    sTpEyZtPbOs
    l2ki6jr624Q
    GycEVtNRVhI
    XUoua5ef63A
    pMl4KCA9YcQ
    
    Code (markup):
    How can I made the new code do that?

    I have it all set up and I have removed all my old code, but I just need to know how to write the information to the text file. I know how to do it the way I was doing it (took me months just to learn that lol).. but this way it isn't writing.
     
    MrLeN, Aug 15, 2008 IP
  5. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #25
    Specifically:

    
    foreach ((array)$_POST['v'] AS $v)
    {
        #echo $v;
    		$string = "".$v."\n";
    }
    
    Code (markup):
    This is the part I don't know how to make written to the new file.

    Maybe I have to have something like:

    
    
    		$string = "foreach ((array)$_POST['v'] AS $v)";
    
    
    Code (markup):
    ?
     
    MrLeN, Aug 15, 2008 IP
  6. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #26
    I got it to print a word into the text file:

    
    Array
    
    Code (markup):
    This is how I made the code. I think I am on the right track, but I am running out of patience.

    
    foreach ((array)$_POST['v'] AS $v)
    {
        $v[0] = $v[0];
        $v[1] = $v[1];
    }
    
    Code (markup):
    I am still trying.. *sigh*

    I think I might just go back to the way I had it. My skills just aren't good enough to get all fancy. It did the job.
     
    MrLeN, Aug 15, 2008 IP
  7. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #27
    I got it working: :D

    
    $listf = fopen ($_SERVER['DOCUMENT_ROOT'] . '/videos/'.$_POST['new_video'].'.txt', 'w+');
    
    foreach ((array)$_POST['v'] AS $v)
    {
        $v = "".$v."\n";
        fwrite($listf,$v);
    }
    
    fclose($listf);
    header('Location: /code.php?v='.$_POST['new_video'].'');
    
    Code (markup):
    I just kept trying everything I can think of until it worked. I really have no idea how it works though :)

    It worked after I put the fwrite INSIDE the foreach brackets. I figured it should go in there because the echo was displaying all the lines, so why wont it just write it? So I figured -- well I'll just put the fwrite in there and see what happens. Then it worked. Man it's really hard to get stuff working when you don't understand the code.
     
    MrLeN, Aug 15, 2008 IP
  8. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #28
    Now I am trying to put the fancy youtube.com detect code in there.

    Am I on the right lines here?

    
    foreach ((array)$_POST['v'] AS $v)
    {
        $v = "".$v."\n";
    $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';
        }
    }		
    		fwrite($listf,$v);
    }
    
    Code (markup):
    I'll play around with it until someone answers. Hopefully I can figure it out myself.
     
    MrLeN, Aug 15, 2008 IP
  9. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #29
    ok I figured it out.

    I was trying to use 2 codes at once - duh, lol.

    This is what I needed:

    
    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];
    				$v = "".$originalUrl[$i]."\n";
    				fwrite($listf,$v);
        } else {
            $originalUrl[$i] = 'EMPTY';
        }
    }
    
    Code (markup):
    Now I am right back to where I started. I need to figure out how to detect if youtube.com is present in the URL. If NOTHING is entered, that's all fine. But if "something" is entered but doesn't contain "youtube.com" I want it to say "Please enter an valid URL" or something.

    ie:

    
    if ( url contains youtube.com ) {
    I put my regular code here
    }
    else {
    ENTER A YOUTUBE URL!
    }
    
    Code (markup):
    I might be able to put a header location in where it says:

    [codde]
    else {
    $originalUrl[$i] = 'EMPTY';
    }
    [/code]

    or something.

    I'll go and try that now.
     
    MrLeN, Aug 15, 2008 IP
  10. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #30
    I just want to say -- thanks heaps for all of you that helped me do this. I am so useless at programming. Thanks for putting up with me. But at least now I have implemented all the code you suggested. I suppose it doesn't really matter too much if the page doesn't say: "Please enter a valid URL".. and the form just writes NOTHING to the file. That amounts to the same thing I guess. The only real problem is people submitting a totally blank form. But I think I can fix that another way by saying something like if $_POST['submit'] != THEN end or return or make a new header location or SOMETHING. I donno, but I think I'll figure it out. I figure all the above out. I have no idea how lol -- but I did :)
     
    MrLeN, Aug 15, 2008 IP