drupal_substr

  1. drupal
    1. 5
    2. 6
    3. 7
    4. 8
Versions
5 – 8 drupal_substr($text, $start, $length = NULL)

Cut off a piece of a string based on character indices and counts. Follows the same behavior as PHP's own substr() function.

Note that for cutting off a string at a known character/substring location, the usage of PHP's normal strpos/substr is safe and much faster.

Related topics

▾ 13 functions call drupal_substr()

DBLogTestCase::doUser in modules/dblog/dblog.test
Generate and verify user events.
drupal_ucfirst in includes/unicode.inc
Capitalize the first letter of a UTF-8 string.
FileExampleSessionStreamWrapper::stream_read in examples/file_example/file_example_session_streams.inc
Support for fread(), file_get_contents() etc.
file_create_url in includes/file.inc
Creates a web-accessible URL for a stream to an external or local file.
ForumTestCase::editForumTaxonomy in modules/forum/forum.test
Edit the forum taxonomy.
menu_contextual_links in includes/menu.inc
Retrieve contextual links for a system object based on registered local tasks.
SearchSimplifyTestCase::testSearchSimplifyUnicode in modules/search/search.test
Tests that all Unicode characters simplify correctly.
search_expand_cjk in modules/search/search.module
Splits CJK (Chinese, Japanese, Korean) text into tokens.
TaxonomyTermTestCase::testTaxonomyGetTermByName in modules/taxonomy/taxonomy.test
Test taxonomy_get_term_by_name().
template_preprocess_username in includes/theme.inc
Preprocesses variables for theme_username().
truncate_utf8 in includes/unicode.inc
Truncates a UTF-8-encoded string safely to a number of characters.
UnicodeUnitTest::helperTestSubStr in modules/simpletest/tests/unicode.test
Updater::findInfoFile in includes/updater.inc
Figure out what the most important (or only) info file is in a directory.

Code

includes/unicode.inc, line 515

<?php
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));
  }
}
?>