سؤال

I'm parsing SSL Certificates using openssl_x509_parse.

The name element of the array produces the following type string which contains the certificate subject information.

[name] = /serialNumber=wsZ0axJazQw/eeFdiFKbpoVW/7uORUSh/OU=GT20702350/OU=See www.rapidssl.com/resources/cps (c)13/OU=Domain Control Validated - RapidSSL(R)/CN=mail.example.com

How could I extract the parts from this string? I'm trying to generate a list using PHP e.g.

serialNumber = wsZ0axJazQw/eeFdiFKbpoVW/7uORUSh
OU = GT20702350
OU = See www.rapidssl.com/resources/cps (c)13
OU = Domain Control Validated - RapidSSL(R)
CN = mail.example.com

I'm still learning how regex's work (they really do give me a headache!), but am struggling with this one, as I'm not sure how to deal with the forward slashes present in some of the 'value' fields of the string.

I've come up with:

([^/]([a-zA-Z0-9]*)[=]([a-zA-Z0-9\s+\.\-\(\)]*)[^/])

So far (http://www.rexfiddle.net/4atGGqG)

As you can see it's very basic, i.e. it doesn't deal with the forward slashes.

How can I 'fix' this?

On a side note, is there a better way (than what I've done) to deal with special characters in the 'value fields' other than an escaped list of each possible one ('\.\-\(\)')?

هل كانت مفيدة؟

المحلول

This regex(using negative lookahead) should work -

\/([a-zA-Z]+?=(?:(?!\/[a-zA-Z]+?=.*?).)*)

You can test it and breakdown the regex Here

Testing it-

$name = "/serialNumber=wsZ0axJazQw/eeFdiFKbpoVW/7uORUSh/OU=GT20702350/OU=See www.rapidssl.com/resources/cps (c)13/OU=Domain Control Validated - RapidSSL(R)/CN=mail.example.com";
$regex = "/\/([a-zA-Z]+?=(?:(?!\/[a-zA-Z]+?=.*?).)*)/";
if(preg_match_all($regex, $name, $matches)){
    var_dump($matches[1]);
}
/**
    OUTPUT
**/
array
  0 => string 'serialNumber=wsZ0axJazQw/eeFdiFKbpoVW/7uORUSh' (length=45)
  1 => string 'OU=GT20702350' (length=13)
  2 => string 'OU=See www.rapidssl.com/resources/cps (c)13' (length=43)
  3 => string 'OU=Domain Control Validated - RapidSSL(R)' (length=41)
  4 => string 'CN=mail.example.com' (length=19)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top