$re = '/(-----BEGIN (?:PUBLIC )?KEY-----(\n|\r|\r\n)([0-9a-zA-Z+\/=]{64}(\n|\r|\r\n))*([0-9a-zA-Z\+\/=]{1,63}(\n|\r|\r\n))?-----END PUBLIC KEY-----)/m';
$str = '-----BEGIN PRIVATE KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAwghnvMu5YfnbprpLX105
SGuanX7zF+4mgNqozyFB7UsIha3ztQIYydrLc7pKXK7vQWWlqLUn0JW1vlAiTs2A
O+wdaFaEwKTNk1+mobGlpKUrzJEy24cnGV9XxNnh+WuxR7TicEpzNOdmDnSUaFMk
MdfpDXKKXfu/HJrADYuMq7VckEBR1yOBXVREDYpS8YvA85Kw04VAPVzxOHU7qdTR
hUPUD1rY3nKYJj33P0EW1e53xqeXZiiW6p3yi04X+OQscylMnfvxsG8tK18Qgi30
cQmny3yYGocKpfG7679Jy5EnkchqoC9L72Ust8A6s0WZVAe/bZxs5J5QEDdAkgAi
Miw84vWIHj2cuwgNKzBJHZ1uHmFrM8rkJWBvLyTRvS1pgn/yjQ4evOTH/yVTGnyM
BOSwrXmFbt4KXzXwtXIf0wllf2HZ3CwhB9g86ijPjvvgXn8eUbBEN12c/MePf3l1
doECe6oavsthqpk77IuFC6Yuy5ck0fkcHT2cw/U6hUBdTbuXs4/i7gMLE9VpjiDh
lYoMCYmuCR8Vn/G5gMQ4NbMjyvZY/d5U8tvktsC2ImQefuWu8vi/PGvDIdhq3TTz
sswoxcLxyqq0vjOnBmpMpua2PpLlZrSuBkuKIuZnxAiRkkLamAVH6SylYwmWvvam
ssssss1
-----END PRIVATE KEY-----
-----BEGIN PUBLIC KEY-----
MIICfzCCAegCCQDxxeXw914Y2DANBgkqhkiG9w0BAQsFADCBgzELMAkGA1UEBhMC
SU4xEzARBgNVBAgMCldlc3RiZW5nYWwxEDAOBgNVBAcMB0tvbGthdGExFDASBgNV
BAoMC1BhbmNvLCBJbmMuMRUwEwYDVQQDDAxSb2hpd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Aw
DgYDVQQHDAdLb2xrYXRhMRQwEgYDVQQKDAtQYW5jbywgSW5jLjEVMBMGA1UEAwwM
Um9oaXQgUHJhc2FkMSAwHgYJKoZIhvcNAQkBFhFyb2ZvZm9mQGdtYWlsLmNvbTCB
nzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAt/EfcF3FG4TneOBWr4JhOUdyuCXm
Dhy5yO3VKtQfPxr+5d0joCSnn/5vYDNSr1MfedZmqVxrXFoMAdPCd71BNmDmeLVi
QK61WREtASP0ZhQMoUBT+R3Fpdy0jPS0YoT/fBd96CJCmgsQOS8Tq5IKVeB61MyC
kwAQ2Goe0T3sdVkCAwEAATANBgkqhkiG9w0BAQsFAAOBgQATe6ixdAjoV7BSHgRX
bXM2+IZLq8kq3s7ck0EZrRVhsivutcaZwDXRCCinB+OlPedbzXwNZGvVX0nwPYHG
BfiXwdiuZeVJ88ni6Fm6RhoPtu2QF1UExfBvSXuMBgR+evp+e3QadNpGx6Ppl1aC
hWF6W2H9+MAlU7yvtmCQQuZmfQ==
-----END CERTIFICATE-----';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php