I was trying to use a regular expression to get the value of the code
parameter of the following url:
"http:\/\/instagram.com\/?code=wz6kmXaZbrDCHU8OdOW0PrshbZF60Zz8Aji4ivaG"
Unfortunately it didn't work out so I had ended up doing the following:
$jsonRedirectUriWithAuthCode = "http:\/\/instagram.com\/?code=wz6kmXaZbrDCHU8OdOW0PrshbZF60Zz8Aji4ivaG";
print "jsonRedirectUriWithAuthCode= " . $jsonRedirectUriWithAuthCode;
print "\n";
$query = parse_url($jsonRedirectUriWithAuthCode, PHP_URL_QUERY);
print "query= " . $query;
print "\n";
parse_str($query, $params);
print "params['code']= ". $params['code'];
die() ;
Which outputs:
jsonRedirectUriWithAuthCode= "http:\/\/instagram.com\/?code=wz6kmXaZbrDCHU8OdOW0PrshbZF60Zz8Aji4ivaG"
query= code=wz6kmXaZbrDCHU8OdOW0PrshbZF60Zz8Aji4ivaG"
params['code']= wz6kmXaZbrDCHU8OdOW0PrshbZF60Zz8Aji4ivaG"
The above does what I wanted to. But I'm still curious how this would be done if using a regular expression. Could someone maybe show me how to extract the value of the code parameter with a regular expression?
Go with
parse_url
andparse_str
. To satisfy your curiosity, you can use something like: