`

drupal的字符串截取函数

 
阅读更多

 

php中的

substr(string,start,length)

该函数对string进行截取操作,截取从字符串的start位置开始,到length长度结束。
利用substr()函数对普通字符进行截取操作会非常方便,但对全角字符进行截取可能会造成乱码。

 

drupal提供了一个安全可靠,又快速的字符串截取函数,无需区分字符是全角还是半角

drupal_substr($text, $start, $length = NULL)

 

function drupal_substr($text, $start, $length = NULL) {
  global $multibyte;
  if ($multibyte == UNICODE_MULTIBYTE) {
    return $length === NULL ? mb_substr($text, $start) : mb_substr($text, $start, $length);
  }
  else {
    $strlen = strlen($text);
    // Find the starting byte offset.
    $bytes = 0;
    if ($start > 0) {
      // Count all the continuation bytes from the start until we have found
      // $start characters or the end of the string.
      $bytes = -1;
      $chars = -1;
      while ($bytes < $strlen - 1 && $chars < $start) {
        $bytes++;
        $c = ord($text[$bytes]);
        if ($c < 0x80 || $c >= 0xC0) {
          $chars++;
        }
      }
    }
    elseif ($start < 0) {
      // Count all the continuation bytes from the end until we have found
      // abs($start) characters.
      $start = abs($start);
      $bytes = $strlen;
      $chars = 0;
      while ($bytes > 0 && $chars < $start) {
        $bytes--;
        $c = ord($text[$bytes]);
        if ($c < 0x80 || $c >= 0xC0) {
          $chars++;
        }
      }
    }
    $istart = $bytes;

    // Find the ending byte offset.
    if ($length === NULL) {
      $iend = $strlen;
    }
    elseif ($length > 0) {
      // Count all the continuation bytes from the starting index until we have
      // found $length characters or reached the end of the string, then
      // backtrace one byte.
      $iend = $istart - 1;
      $chars = -1;
      $last_real = FALSE;
      while ($iend < $strlen - 1 && $chars < $length) {
        $iend++;
        $c = ord($text[$iend]);
        $last_real = FALSE;
        if ($c < 0x80 || $c >= 0xC0) {
          $chars++;
          $last_real = TRUE;
        }
      }
      // Backtrace one byte if the last character we found was a real character
      // and we don't need it.
      if ($last_real && $chars >= $length) {
        $iend--;
      }
    }
    elseif ($length < 0) {
      // Count all the continuation bytes from the end until we have found
      // abs($start) characters, then backtrace one byte.
      $length = abs($length);
      $iend = $strlen;
      $chars = 0;
      while ($iend > 0 && $chars < $length) {
        $iend--;
        $c = ord($text[$iend]);
        if ($c < 0x80 || $c >= 0xC0) {
          $chars++;
        }
      }
      // Backtrace one byte if we are not at the beginning of the string.
      if ($iend > 0) {
        $iend--;
      }
    }
    else {
      // $length == 0, return an empty string.
      return '';
    }

    return substr($text, $istart, max(0, $iend - $istart + 1));
  }
}

 

 

 

分享到:
评论

相关推荐

    Drupal7之drupal_static函数用法解析

    主要为大家介绍了Drupal7之drupal_static函数用法,需要的朋友可以参考下

    Drupal7宝典+Drupal开发指南+Using Drupal

    包含:Drupal7宝典; Drupal开发指南; Using Drupal(强烈推荐) 值得你下载!

    Drupal data Drupal data

    Drupal dataDrupal data

    Enterprise Drupal 8 Development

    "Enterprise Drupal 8 Development: For Advanced Projects and Large Development Teams" English | ISBN: 1484202546 | 2017 | 309 pages | PDF | 9 MB Successfully architect a Drupal 8 website that scales ...

    drupal常用判断函数汇总

    主要为大家介绍了drupal常用判断函数,实例汇总了判断首页、判断用户角色、判断权限等常用函数,具有一定的参考借鉴价值,需要的朋友可以参考下

    drupal PHPTemplate主题引擎

    Drupal的主题化函数文档可参看Development Plumbing站点。每个模板文件都包含了一个HTML骨架,里面嵌入了一些简单的PHP语句,用来输出动态数据。因此,如果你仅仅了解一点PHP的话,那么PHPTemplate应该是你的很好的...

    Drupal 7.54 英文版.zip

    添加了一个新的drupal_is_https()API函数;搜索记录现在可以被禁用(管理界面中的新选项;日期类型配置页面上的短和中日期格式的默认值现在是正确的;文件验证错误消息现在在后续上传有效文件后被删除。

    drupal6版本(这是drupal6)

    drupal6的安装,drupal6的安装drupal6的安装drupal6的安装

    Decoupled Drupal in Practice

    Decoupled Drupal in Practice: Architect and Implement Decoupled Drupal Architectures Across the Stack By 作者: Preston So ISBN-10 书号: 1484240715 ISBN-13 书号: 9781484240717 Edition 版本: 1st ed. ...

    drupal7与drupal6版本修改内容

    drupal7 vs drupal6 详细的列出了从drupal6升级到drupal7所做的一些改动。 从代码,配置,UI,API等全方面的诠释drupal7与drupal6 的不同之处。

    Drupal中hook_theme函数用法

    主要为大家介绍了Drupal中hook_theme函数用法,可以实现对drupal主题的灵活定制,对于drupal建站来说非常具有实用价值,需要的朋友可以参考下

    Beginning Drupal 8(Apress,2015)

    Beginning Drupal 8 teaches you how to build, maintain, and manage Drupal 8-based web sites. The book covers what Drupal is, using Drupal when building a new web site, installing and configuring Drupal...

    drupal中创建hook_user并调用drupal的mail函数发送email实例

    主要为大家介绍了drupal中创建hook_user并调用drupal的mail函数发送email实现方法,涉及hook函数的用法及mail函数的调用方法,需要的朋友可以参考下

    drupal常用到的几种过滤函数小结

    主要为大家介绍了drupal常用到的几种过滤函数,总结了常见过滤函数的具体用法,非常实用,具有一定的参考借鉴价值,需要的朋友可以参考下

    零起点学习Drupal教程

    零起点学习Drupal教程零起点学习Drupal教程零起点学习Drupal教程

    Drupal 8 Module Development 2nd Edition

    Write a Drupal 8 module with custom functionality and hook into various extension points Master numerous Drupal 8 sub-systems and APIs Model, store, and manipulate data in various ways and for various...

    Drupal6开发手册

    Drupal6开发手册Drupal6开发手册Drupal6开发手册Drupal6开发手册Drupal6开发手册Drupal6开发手册Drupal6开发手册

    Drupal专业开发指南(Drupal5)

    Drupal专业开发指南,这是drupal5版的。可能现在还有人用。

    [Drupal] Drupal 响应式主题 开发教程 (英文版)

    Ideal for experienced Drupal developers, this book takes you through RWD basics and shows you how to build sites based on Aurora, Zen, and Omega—three popular base themes created by Drupal ...

    Drupal 7 高级开发第三版 Drupal7专业开发指南

    Drupal 7 高级开发第三版 Drupal7专业开发指南

Global site tag (gtag.js) - Google Analytics