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?
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?
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.
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 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
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! } ?
Why PM? Are you gonna charge him for one line of code? 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? )
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 Nope, I am "the" MrLeN online. I own the dot com -- so there you go lol.
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 Does the job!
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.
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:
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.
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
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.