Jump to content

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
https://forums.phpfreaks.com/topic/269859-extracting-a-substring-from-a-string/
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

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?  

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.

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

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.

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.

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.