Preg_match_all-php Regular Expression Problems

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.