npsari Posted October 24, 2012 Share Posted October 24, 2012 (edited) Hello, I am trying to extract some letters from a Youtube URL I want to extract the letters between '=' and '&' Which are 'ruJ1uFf9hy4' The code I have does not do the whole job :-\ $string = "http://www.youtube.com/watch?v=ruJ1uFf9hy4&feature=g-vrec"; $result = substr($string, 0, strpos($string, "=")); echo "$result"; /// Will echo http://www.youtube.com/watch?v Please give code! Edited October 24, 2012 by npsari Quote Link to comment https://forums.phpfreaks.com/topic/269859-extracting-a-substring-from-a-string/ Share on other sites More sharing options...
Jessica Posted October 24, 2012 Share Posted October 24, 2012 $string = "http://www.youtube.com/watch?v=ruJ1uFf9hy4&feature=g-vrec"; $result = substr($string, strpos($string, "=")); $result = substr($result, 0, strpos($string, "&")); Quote Link to comment https://forums.phpfreaks.com/topic/269859-extracting-a-substring-from-a-string/#findComment-1387461 Share on other sites More sharing options...
ManiacDan Posted October 24, 2012 Share Posted October 24, 2012 You could also use parse_url for this. Quote Link to comment https://forums.phpfreaks.com/topic/269859-extracting-a-substring-from-a-string/#findComment-1387465 Share on other sites More sharing options...
npsari Posted October 24, 2012 Author Share Posted October 24, 2012 (edited) Thank you Jessica But your result is >>>>>>>>>>> =ruJ1uFf9hy4&feature=g-vrec I want this >>>>>>>>>>>>>> ruJ1uFf9hy4 Moderator, why not share the script, you sound like an expert Edited October 24, 2012 by npsari Quote Link to comment https://forums.phpfreaks.com/topic/269859-extracting-a-substring-from-a-string/#findComment-1387466 Share on other sites More sharing options...
Christian F. Posted October 24, 2012 Share Posted October 24, 2012 Try clicking the link, and everything should be explained quite nicely. Quote Link to comment https://forums.phpfreaks.com/topic/269859-extracting-a-substring-from-a-string/#findComment-1387470 Share on other sites More sharing options...
npsari Posted October 24, 2012 Author Share Posted October 24, 2012 $a = "http://www.youtube.com/watch?v=ruJ1uFf9hy4&feature=g-vrec"; $a = split('[=&]', $a); echo $a[1]; Quote Link to comment https://forums.phpfreaks.com/topic/269859-extracting-a-substring-from-a-string/#findComment-1387473 Share on other sites More sharing options...
ManiacDan Posted October 24, 2012 Share Posted October 24, 2012 I have no script to share, I linked you to the manual page of a function which does exactly what you're asking for. Read the page, learn something new, and apply that knowledge to write a script. Speaking of the manual, did you happen to read the manual page for the split function you're using? See the big red box that says using the function is highly discouraged? Quote Link to comment https://forums.phpfreaks.com/topic/269859-extracting-a-substring-from-a-string/#findComment-1387475 Share on other sites More sharing options...
npsari Posted October 24, 2012 Author Share Posted October 24, 2012 Really? Why is it discouraged? It is amazing, the script i posted is doing the job, please explain! Quote Link to comment https://forums.phpfreaks.com/topic/269859-extracting-a-substring-from-a-string/#findComment-1387476 Share on other sites More sharing options...
ManiacDan Posted October 24, 2012 Share Posted October 24, 2012 This function has been DEPRECATED as of PHP 5.3.0.That's why it's discouraged. The manual is absolutely the best PHP resource you can get. You have to read it. Quote Link to comment https://forums.phpfreaks.com/topic/269859-extracting-a-substring-from-a-string/#findComment-1387477 Share on other sites More sharing options...
npsari Posted October 24, 2012 Author Share Posted October 24, 2012 I never understood the PHP versions and packs, I use all PHP codes and they always work, never got an error message telling me this PHP code is too old. So you mean this script will not work in the future? Quote Link to comment https://forums.phpfreaks.com/topic/269859-extracting-a-substring-from-a-string/#findComment-1387479 Share on other sites More sharing options...
ManiacDan Posted October 24, 2012 Share Posted October 24, 2012 If you don't get an error when you run this code, you have errors turned off, and that's bad. As a language grows (any language), new features are added. Some of those new features are better, more efficient, or more secure versions of old features. The old features are then deprecated (marked as "old"). After a few more version, deprecated functions are removed entirely. Functions like ereg, split, and register_globals, as well as variables like $HTTP_POST_VARS are deprecated. Soon, they'll be gone. Right now, your code throws an error. Maybe when PHP6 comes out, it will stop working. Just because something is possible doesn't mean it's the best way to do something. You're trying to parse a URL. The function I pointed you to is called "parse url." Using a deprecated regular expression engine (which is really slow) to split it into uneven chunks in the hopes that it comes out with the right answer is just...not going to work out well for you. For instance, this is a valid youtube URL: http://www.youtube.com/watch?v=ruJ1uFf9hy4#t=2m14s Your code would break on that. Using parse_url would not. Quote Link to comment https://forums.phpfreaks.com/topic/269859-extracting-a-substring-from-a-string/#findComment-1387487 Share on other sites More sharing options...
npsari Posted October 24, 2012 Author Share Posted October 24, 2012 Thank you for the information, I thought the problem is solved :-\ I really loved this split function!!! I tried using parse_url() is giving me the whole thing v=ruJ1uFf9hy4&feature=g-vrec If you have some free time, why not share script, and I will do a favor back to you Quote Link to comment https://forums.phpfreaks.com/topic/269859-extracting-a-substring-from-a-string/#findComment-1387492 Share on other sites More sharing options...
ManiacDan Posted October 24, 2012 Share Posted October 24, 2012 If you really love split, I have good news for you: Actually reading the manual page would give you 4 alternatives to split that actually work properly and aren't deprecated. Parse_url pulls out the query string, then parse_str will parse the query string for you. That's the only correct way to do this. You can continue splitting the string if you want, but I've already demonstrated a valid URL which will break your method and not mine. Quote Link to comment https://forums.phpfreaks.com/topic/269859-extracting-a-substring-from-a-string/#findComment-1387494 Share on other sites More sharing options...
npsari Posted October 24, 2012 Author Share Posted October 24, 2012 ohhhh, ok ok, i get it, thank you for information By the way, what manual page? I thought you meant the link you are giving me where is this manual page you talking about? Quote Link to comment https://forums.phpfreaks.com/topic/269859-extracting-a-substring-from-a-string/#findComment-1387498 Share on other sites More sharing options...
akphidelt2007 Posted October 24, 2012 Share Posted October 24, 2012 ohhhh, ok ok, i get it, thank you for information By the way, what manual page? I thought you meant the link you are giving me where is this manual page you talking about? That is the PHP manual. Outside of espn.com that should be the most visited page you'll visit in your life. Quote Link to comment https://forums.phpfreaks.com/topic/269859-extracting-a-substring-from-a-string/#findComment-1387504 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.