当前位置: 首页>>代码示例>>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;未经允许,请勿转载。