Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. AbstractString.php

function AbstractString::toByteString

File

vendor/symfony/string/AbstractString.php, line 503

Class

AbstractString
Represents a string of abstract characters.

Namespace

Symfony\Component\String

Code

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;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal