Reputation: 1279
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?
Upvotes: 1
Views: 819
Reputation: 98991
Go with parse_url
and parse_str
.
To satisfy your curiosity, you can use something like:
preg_match_all('/code=(.*?)$/', $theUrl, $match);
echo $match[1][0];
Upvotes: 1