public static function fromString($headerLine) { list($fieldName, $fieldValue) = GenericHeader::splitHeaderLine($headerLine); $decodedValue = HeaderWrap::mimeDecodeValue($fieldValue); $wasEncoded = $decodedValue !== $fieldValue; $fieldValue = $decodedValue; if (strtolower($fieldName) !== static::$type) { throw new Exception\InvalidArgumentException(sprintf('Invalid header line for "%s" string', __CLASS__)); } $header = new static(); if ($wasEncoded) { $header->setEncoding('UTF-8'); } // split value on "," $fieldValue = str_replace(Headers::FOLDING, ' ', $fieldValue); $fieldValue = preg_replace('/[^:]+:([^;]*);/', '$1,', $fieldValue); $values = str_getcsv($fieldValue, ','); array_walk($values, function (&$value) { $value = trim($value); $value = self::stripComments($value); }); $values = array_filter($values); $addressList = $header->getAddressList(); foreach ($values as $address) { $addressList->addFromString($address); } return $header; }
/** * Parse string to create header object * * @param string $headerLine * @throws Exception\InvalidArgumentException * @return AbstractAddressList */ public static function fromString($headerLine) { $headerLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR); // split into name/value list($fieldName, $fieldValue) = explode(': ', $headerLine, 2); if (strtolower($fieldName) !== static::$type) { throw new Exception\InvalidArgumentException(sprintf( 'Invalid header line for "%s" string', __CLASS__ )); } $header = new static(); // split value on "," $fieldValue = str_replace("\r\n ", " ", $fieldValue); $values = explode(',', $fieldValue); array_walk($values, 'trim'); $addressList = $header->getAddressList(); foreach ($values as $address) { // split values into name/email if (!preg_match('/^((?P<name>.*?)<(?P<namedEmail>[^>]+)>|(?P<email>.+))$/', $address, $matches)) { // Should we raise an exception here? continue; } $name = null; if (isset($matches['name'])) { $name = trim($matches['name']); } if (empty($name)) { $name = null; } else { $name = iconv_mime_decode($name, ICONV_MIME_DECODE_CONTINUE_ON_ERROR); } if (isset($matches['namedEmail'])) { $email = $matches['namedEmail']; } if (isset($matches['email'])) { $email = $matches['email']; } $email = trim($email); // we may have leading whitespace // populate address list $addressList->add($email, $name); } return $header; }
public static function fromString($headerLine) { list($fieldName, $fieldValue) = GenericHeader::splitHeaderLine($headerLine); $decodedValue = HeaderWrap::mimeDecodeValue($fieldValue); $wasEncoded = $decodedValue !== $fieldValue; $fieldValue = $decodedValue; if (strtolower($fieldName) !== static::$type) { throw new Exception\InvalidArgumentException(sprintf('Invalid header line for "%s" string', __CLASS__)); } $header = new static(); if ($wasEncoded) { $header->setEncoding('UTF-8'); } // split value on "," $fieldValue = str_replace(Headers::FOLDING, ' ', $fieldValue); $values = explode(',', $fieldValue); array_walk($values, function (&$value) { $value = trim($value); }); $addressList = $header->getAddressList(); foreach ($values as $address) { // split values into name/email if (!preg_match('/^((?P<name>.*?)<(?P<namedEmail>[^>]+)>|(?P<email>.+))$/', $address, $matches)) { // Should we raise an exception here? continue; } $name = null; if (isset($matches['name'])) { $name = trim($matches['name']); } if (empty($name)) { $name = null; } if (isset($matches['namedEmail'])) { $email = $matches['namedEmail']; } if (isset($matches['email'])) { $email = $matches['email']; } $email = trim($email); // we may have leading whitespace // populate address list $addressList->add($email, $name); } return $header; }
public static function fromString($headerLine) { $decodedLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8'); // split into name/value list($fieldName, $fieldValue) = GenericHeader::splitHeaderLine($decodedLine); if (strtolower($fieldName) !== static::$type) { throw new Exception\InvalidArgumentException(sprintf('Invalid header line for "%s" string', __CLASS__)); } $header = new static(); if ($decodedLine != $headerLine) { $header->setEncoding('UTF-8'); } // split value on "," $fieldValue = str_replace(Headers::FOLDING, ' ', $fieldValue); $values = str_getcsv($fieldValue, ','); array_walk($values, function (&$value) { $value = trim($value); }); $addressList = $header->getAddressList(); foreach ($values as $address) { $addressList->addFromString($address); } return $header; }