phppreg-match-alltext-extractionauthorization-header

Extract significant parts of an AWS authorization header string


My input string:

AWS-HMAC-SHA256 Credential=eyJhbGciOiJIUzI1NiIsIngtc3MiOjEy/20160911/cn/user-service/request,SignedHeaders=host;x-aws-date, Signature=d9ee2d43f2067e4b8857f15fa8fff27820051d95a4ec31e93be866f201e0797a

How can I get the values for Credential, SignedHeaders, and Signature?


Solution

  • Instead of using a regex, you might use explode and array_map:

    $str = "AWS-HMAC-SHA256 Credential=eyJhbGciOiJIUzI1NiIsIngtc3MiOjEy/20160911/cn/user-service/request,SignedHeaders=host;x-aws-date, Signature=d9ee2d43f2067e4b8857f15fa8fff27820051d95a4ec31e93be866f201e0797a";
    $res = array_map(function($x){
        return explode('=', $x)[1];
    }, explode(',', $str));
    print_r($res);
    

    Result:

    Array
    (
        [0] => eyJhbGciOiJIUzI1NiIsIngtc3MiOjEy/20160911/cn/user-service/request
        [1] => host;x-aws-date
        [2] => d9ee2d43f2067e4b8857f15fa8fff27820051d95a4ec31e93be866f201e0797a
    )
    

    Demo