get_string_between() public static method

Extracts a substring between 2 marks
public static get_string_between ( string $str, string $start, string $end ) : string
$str string The target string
$start string The initial mark
$end string The end mark
return string A substring or an empty string if is not able to find the marks or if there is no string between the marks
Example #1
0
 /**
  * Returns a private key (adding header & footer if required).
  *
  * @param string  $key   A private key
  * @param bool    $heads True if we want to include head and footer
  *
  * @return string $rsaKey Formatted private key
  */
 public static function formatPrivateKey($key, $heads = true)
 {
     $key = str_replace(array("\r", "\r", "\n"), "", $key);
     if (!empty($key)) {
         if (strpos($key, '-----BEGIN PRIVATE KEY-----') !== false) {
             $key = OneLogin_Saml2_Utils::get_string_between($key, '-----BEGIN PRIVATE KEY-----', '-----END PRIVATE KEY-----');
             $key = str_replace(' ', '', $key);
             if ($heads) {
                 $key = "-----BEGIN PRIVATE KEY-----\n" . chunk_split($key, 64, "\n") . "-----END PRIVATE KEY-----\n";
             }
         } else {
             if (strpos($key, '-----BEGIN RSA PRIVATE KEY-----') !== false) {
                 $key = OneLogin_Saml2_Utils::get_string_between($key, '-----BEGIN RSA PRIVATE KEY-----', '-----END RSA PRIVATE KEY-----');
                 $key = str_replace(' ', '', $key);
                 if ($heads) {
                     $key = "-----BEGIN RSA PRIVATE KEY-----\n" . chunk_split($key, 64, "\n") . "-----END RSA PRIVATE KEY-----\n";
                 }
             } else {
                 $key = str_replace(' ', '', $key);
                 if ($heads) {
                     $key = "-----BEGIN RSA PRIVATE KEY-----\n" . chunk_split($key, 64, "\n") . "-----END RSA PRIVATE KEY-----\n";
                 }
             }
         }
     }
     return $key;
 }