Jump to content

Extracting A Substring From A String


npsari

Recommended Posts

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 by npsari
Link to comment
Share on other sites

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 by npsari
Link to comment
Share on other sites

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?  

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.