0

I have a string like so:

http://www.youtube.com/v/Nnp82q3b844&hl=en_US&fs=1&

and I want to extract the

Nnp82q3b844

part of it i.e. the part between /v/ and the first &.

Is there and easy way to do this in PHP?

1

3 Answers 3

7

You don't necessary need regular expressions in this case, you can use the parse_url function to do the work.

<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';

print_r(parse_url($url));

echo parse_url($url, PHP_URL_PATH);
?>

The above example will output:

Array
(
   [scheme] => http
   [host] => hostname
   [user] => username
   [pass] => password
   [path] => /path
   [query] => arg=value
   [fragment] => anchor
)
/path
Sign up to request clarification or add additional context in comments.

2 Comments

@Pentuim10: good function, I didn't use it, I have learn something today thanks to you!
@RageZ Thanks. Look into my other answers, on my profile, that have been upvoted, you may find more to learn.
2

yes he shouldn't be too hard take a look here for you reference or to understand my answer

after for the regular expression

something like this should make it

preg_match('|http://www.youtube.com/v/([^&]+)&hl=en_US&fs=1&|', $url, $match );
var_dump($match[1]);

The [^&]+ means basically more than or one character that is not a '&', '[]' define some character possibilities [^] make it reverse so any character not in the bracket, + mean 1 or more character.

But you have better to look it by yourself!

I really advise you to take a good look at regular expressions because it can really save you hours of work and once you get how it works, it is really useful!

Comments

1
$str="http://www.youtube.com/v/Nnp82q3b844&hl=en_US&fs=1&";
$s = parse_url($str);
$t = explode("/", $s["path"]);
print preg_replace("/&.*/","",end($t));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.