當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Unicode::truncateBytes方法代碼示例

本文整理匯總了PHP中Drupal\Component\Utility\Unicode::truncateBytes方法的典型用法代碼示例。如果您正苦於以下問題:PHP Unicode::truncateBytes方法的具體用法?PHP Unicode::truncateBytes怎麽用?PHP Unicode::truncateBytes使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Drupal\Component\Utility\Unicode的用法示例。


在下文中一共展示了Unicode::truncateBytes方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: query

 /**
  * {@inheritdoc}
  */
 public function query($query, array $args = array(), $options = array())
 {
     try {
         return parent::query($query, $args, $options);
     } catch (DatabaseException $e) {
         if ($e->getPrevious()->errorInfo[1] == 1153) {
             // If a max_allowed_packet error occurs the message length is truncated.
             // This should prevent the error from recurring if the exception is
             // logged to the database using dblog or the like.
             $message = Unicode::truncateBytes($e->getMessage(), self::MIN_MAX_ALLOWED_PACKET);
             $e = new DatabaseExceptionWrapper($message, $e->getCode(), $e->getPrevious());
         }
         throw $e;
     }
 }
開發者ID:penguinclub,項目名稱:penguinweb_drupal8,代碼行數:18,代碼來源:Connection.php

示例2: convert

  /**
   * Converts a value between two search types.
   *
   * @param mixed $value
   *   The value to convert.
   * @param string $type
   *   The type to convert to. One of the keys from
   *   search_api_default_field_types().
   * @param string $original_type
   *   The value's original type.
   * @param \Drupal\search_api\IndexInterface $index
   *   The index for which this conversion takes place.
   *
   * @return mixed
   *   The converted value.
   *
   * @throws \Drupal\search_api\SearchApiException
   *   Thrown if $type is unknown.
   */
  protected function convert($value, $type, $original_type, IndexInterface $index) {
    if (!isset($value)) {
      // For text fields, we have to return an array even if the value is NULL.
      return Utility::isTextType($type, array('text', 'tokenized_text')) ? array() : NULL;
    }
    switch ($type) {
      case 'text':
        // For dates, splitting the timestamp makes no sense.
        if ($original_type == 'date') {
          $value = format_date($value, 'custom', 'Y y F M n m j d l D');
        }
        $ret = array();
        foreach (preg_split('/[^\p{L}\p{N}]+/u', $value, -1, PREG_SPLIT_NO_EMPTY) as $v) {
          if ($v) {
            if (strlen($v) > 50) {
              $this->getLogger()->warning('An overlong word (more than 50 characters) was encountered while indexing: %word.<br />Database search servers currently cannot index such words correctly – the word was therefore trimmed to the allowed length. Ensure you are using a tokenizer preprocessor.', array('%word' => $v));
              $v = Unicode::truncateBytes($v, 50);
            }
            $ret[] = array(
              'value' => $v,
              'score' => 1,
            );
          }
        }
        // This used to fall through the tokenized case
        return $ret;

      case 'tokenized_text':
        while (TRUE) {
          foreach ($value as $i => $v) {
            // Check for over-long tokens.
            $score = $v['score'];
            $v = $v['value'];
            if (strlen($v) > 50) {
              $words = preg_split('/[^\p{L}\p{N}]+/u', $v, -1, PREG_SPLIT_NO_EMPTY);
              if (count($words) > 1 && max(array_map('strlen', $words)) <= 50) {
                // Overlong token is due to bad tokenizing.
                // Check for "Tokenizer" preprocessor on index.
                if (empty($index->getOption('processors')['search_api_tokenizer']['status'])) {
                  $this->getLogger()->warning('An overlong word (more than 50 characters) was encountered while indexing, due to bad tokenizing. It is recommended to enable the "Tokenizer" preprocessor for indexes using database servers. Otherwise, the backend class has to use its own, fixed tokenizing.');
                }
                else {
                  $this->getLogger()->warning('An overlong word (more than 50 characters) was encountered while indexing, due to bad tokenizing. Please check your settings for the "Tokenizer" preprocessor to ensure that data is tokenized correctly.');
                }
              }

              $tokens = array();
              foreach ($words as $word) {
                if (strlen($word) > 50) {
                  $this->getLogger()->warning('An overlong word (more than 50 characters) was encountered while indexing: %word.<br />Database search servers currently cannot index such words correctly – the word was therefore trimmed to the allowed length.', array('%word' => $word));
                  $word = Unicode::truncateBytes($word, 50);
                }
                $tokens[] = array(
                  'value' => $word,
                  'score' => $score,
                );
              }
              array_splice($value, $i, 1, $tokens);
              // Restart the loop looking through all the tokens.
              continue 2;
            }
          }
          break;
        }
        return $value;

      case 'string':
      case 'uri':
        // For non-dates, PHP can handle this well enough.
        if ($original_type == 'date') {
          return date('c', $value);
        }
        if (strlen($value) > 255) {
          $value = Unicode::truncateBytes($value, 255);
          $this->getLogger()->warning('An overlong value (more than 255 characters) was encountered while indexing: %value.<br />Database search servers currently cannot index such values correctly – the value was therefore trimmed to the allowed length.', array('%value' => $value));
        }
        return $value;

      case 'integer':
      case 'duration':
      case 'decimal':
//.........這裏部分代碼省略.........
開發者ID:jkyto,項目名稱:agolf,代碼行數:101,代碼來源:Database.php

示例3: testTruncateBytes

 /**
  * Tests multibyte truncate bytes.
  *
  * @dataProvider providerTestTruncateBytes
  * @covers ::truncateBytes
  *
  * @param string $text
  *   The string to truncate.
  * @param int $max_length
  *   The upper limit on the returned string length.
  * @param string $expected
  *   The expected return from Unicode::truncateBytes().
  */
 public function testTruncateBytes($text, $max_length, $expected)
 {
     $this->assertEquals($expected, Unicode::truncateBytes($text, $max_length), 'The string was not correctly truncated.');
 }
開發者ID:Nikola-xiii,項目名稱:d8intranet,代碼行數:17,代碼來源:UnicodeTest.php

示例4: prepareComment

 public function prepareComment($comment, $length = NULL) {
   // Truncate comment to maximum comment length.
   if (isset($length)) {
     // Add table prefixes before truncating.
     $comment = Unicode::truncateBytes($this->connection->prefixTables($comment), $length, TRUE, TRUE);
   }
   return $this->connection->quote($comment);
 }
開發者ID:HamzaBendidane,項目名稱:prel,代碼行數:8,代碼來源:Schema.php


注:本文中的Drupal\Component\Utility\Unicode::truncateBytes方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。