function AbstractString::toByteString
File
-
vendor/
symfony/ string/ AbstractString.php, line 503
Class
- AbstractString
- Represents a string of abstract characters.
Namespace
Symfony\Component\StringCode
public function toByteString(?string $toEncoding = null) : ByteString {
$b = new ByteString();
$toEncoding = \in_array($toEncoding, [
'utf8',
'utf-8',
'UTF8',
], true) ? 'UTF-8' : $toEncoding;
if (null === $toEncoding || $toEncoding === ($fromEncoding = $this instanceof AbstractUnicodeString || preg_match('//u', $b->string) ? 'UTF-8' : 'Windows-1252')) {
$b->string = $this->string;
return $b;
}
try {
$b->string = mb_convert_encoding($this->string, $toEncoding, 'UTF-8');
} catch (\ValueError $e) {
if (!\function_exists('iconv')) {
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
}
$b->string = iconv('UTF-8', $toEncoding, $this->string);
}
return $b;
}