How do I match 9ecfd84450f7d5d40e013cf84d0979ff of openid in {code? How to match
$rs='callback( {"client_id":"101223150","openid":"9ECFD84190F7D5D40E013CF84D0979FF"} );';
In openid 9ecfd84425f7d5d40e013cf84d0979ff?
Reply content:
How to match
$rs='callback( {"client_id":"101223150","openid":"9ECFD84190F7D5D40E013CF84D0979FF"} );';
In openid 9ecfd84425f7d5d40e013cf84d0979ff?
You can use regular expression lookup.
For example, match is required.
Hello world
In
Hello world, The regular expression can be written as follows:
(?<=
)(.*)(?=
)
ThereforeopenidThe value can be as follows:
Output:
Array( [0] => 9ECFD84190F7D5D40E013CF84D0979FF [1] => 9ECFD84190F7D5D40E013CF84D0979FF)
phppreg_match_all('@callback\((.*)\);@', $rs,$matches );$arr = json_decode($matches[1][0],true);print_r($arr['openid']);//9ECFD84190F7D5D40E013CF84D0979FF
This is a standard jsonp format (json parameter + callback function). A simple solution is to take out the callback function () and retrieve its parameter section and parse it as json:
php$ Rs = 'callback ({"client_id": "101223150", "openid": "9ecfd84369f7d5d40e013cf84d0979ff"}); '; $ jsonp = []; preg_match ('/callback \((. +) \);/', $ rs, $ jsonp); print_r ($ jsonp); $ result = json_decode ($ jsonp [1]); print_r ($ result ); // use $ result-> openid to access the data you need
Output
Array( [0] => callback( {"client_id":"101223150","openid":"9ECFD84190F7D5D40E013CF84D0979FF"} ); [1] => {"client_id":"101223150","openid":"9ECFD84190F7D5D40E013CF84D0979FF"})stdClass Object( [client_id] => 101223150 [openid] => 9ECFD84190F7D5D40E013CF84D0979FF)
The Code is as follows:
php$rs='callback( {"client_id":"101223150","openid":"9ECFD84190F7D5D40E013CF84D0979FF"} );';$preg = '/openid["|\']:["|\'](\w+)/i';preg_match($preg,$rs,$result);echo $result[1];//9ECFD84190F7D5D40E013CF84D0979FF