class PHPUnit_Util_String

String helpers.

@package PHPUnit @subpackage Util @author Sebastian Bergmann <sebastian@phpunit.de> @copyright 2001-2013 Sebastian Bergmann <sebastian@phpunit.de> @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License @link http://www.phpunit.de/ @since Class available since Release 3.6.0

Hierarchy

Expanded class hierarchy of PHPUnit_Util_String

File

drupal/core/vendor/phpunit/phpunit/PHPUnit/Util/String.php, line 57

View source
class PHPUnit_Util_String {

  /**
   * Converts a string to UTF-8 encoding.
   *
   * @param  string $string
   * @return string
   */
  public static function convertToUtf8($string) {
    if (!self::isUtf8($string)) {
      if (function_exists('mb_convert_encoding')) {
        $string = mb_convert_encoding($string, 'UTF-8');
      }
      else {
        $string = utf8_encode($string);
      }
    }
    return $string;
  }

  /**
   * Checks a string for UTF-8 encoding.
   *
   * @param  string $string
   * @return boolean
   */
  protected static function isUtf8($string) {
    $length = strlen($string);
    for ($i = 0; $i < $length; $i++) {
      if (ord($string[$i]) < 0x80) {
        $n = 0;
      }
      else {
        if ((ord($string[$i]) & 0xe0) == 0xc0) {
          $n = 1;
        }
        else {
          if ((ord($string[$i]) & 0xf0) == 0xe0) {
            $n = 2;
          }
          else {
            if ((ord($string[$i]) & 0xf0) == 0xf0) {
              $n = 3;
            }
            else {
              return FALSE;
            }
          }
        }
      }
      for ($j = 0; $j < $n; $j++) {
        if (++$i == $length || (ord($string[$i]) & 0xc0) != 0x80) {
          return FALSE;
        }
      }
    }
    return TRUE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PHPUnit_Util_String::convertToUtf8 public static function Converts a string to UTF-8 encoding.
PHPUnit_Util_String::isUtf8 protected static function Checks a string for UTF-8 encoding.