devWhiz Posted June 9, 2012 Share Posted June 9, 2012 how can I use preg_match to take aHjpOzsQ9YI out of www.youtube.com/watch?v=aHjpOzsQ9YI, I am still unfamiliar with regex, I've been writing php for alomost 4 years now and still don't know regex lol Any help is appreciated.. Thanks -CLUEL3SS Quote Link to comment Share on other sites More sharing options...
requinix Posted June 9, 2012 Share Posted June 9, 2012 parse_url followed by parse_str. No regex needed. Quote Link to comment Share on other sites More sharing options...
MarPlo Posted June 9, 2012 Share Posted June 9, 2012 Hi, Try this code: $url = 'www.youtube.com/watch?v=aHjpOzsQ9YI'; preg_match('/v=([a-z0-9]+)$/i', $url, $mc); echo $mc[1]; // aHjpOzsQ9YI Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted June 9, 2012 Share Posted June 9, 2012 Hi, Try this code: $url = 'www.youtube.com/watch?v=aHjpOzsQ9YI'; preg_match('/v=([a-z0-9]+)$/i', $url, $mc); echo $mc[1]; // aHjpOzsQ9YI That won't always work, youtube uses other characters as well. as requinix said do this: $url = "www.youtube.com/watch?v=aHjpOzsQ9YI"; $pu = parse_url($url); // php 5.4: $query = parse_url($url)["query"]; // other: $query = $pu["query"]; $parameters = parse_str($query); print_r($parameters); Quote Link to comment Share on other sites More sharing options...
.josh Posted June 22, 2012 Share Posted June 22, 2012 TLG parse_url() accepts an optional 2nd argument to only return the portion you want. Also, parse_str() returns void. You have to specify the array as the 2nd argument. url = "www.youtube.com/watch?v=aHjpOzsQ9YI"; $query = parse_url($url,PHP_URL_QUERY); parse_str($query,$params); echo $params['v']; Quote Link to comment 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.