当前位置: 首页>>代码示例>>PHP>>正文


PHP CString::substring方法代码示例

本文整理汇总了PHP中CString::substring方法的典型用法代码示例。如果您正苦于以下问题:PHP CString::substring方法的具体用法?PHP CString::substring怎么用?PHP CString::substring使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CString的用法示例。


在下文中一共展示了CString::substring方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: wordWrap

 /**
  * Wraps the text in a string to a specified width and returns the new string.
  *
  * @param  string $string The string with the text to be wrapped.
  * @param  int $width The wrapping width, in characters.
  * @param  bitfield $wrappingFlags **OPTIONAL. Default is** `WRAPPING_DEFAULT`. The wrapping option(s). The
  * available options are `WRAPPING_BREAK_SPACELESS_LINES`, `WRAPPING_ALLOW_TRAILING_SPACES`,
  * `WRAPPING_DISALLOW_LEADING_SPACES`, and `WRAPPING_DONT_BREAK_SPACELESS_CJK_ENDING_LINES`
  * (see [Summary](#summary)).
  * @param  string $newline **OPTIONAL. Default is** LF (U+000A). The newline character(s) to be used for making
  * new lines in the process of wrapping.
  *
  * @return string The wrapped text.
  */
 public static function wordWrap($string, $width, $wrappingFlags = self::WRAPPING_DEFAULT, $newline = self::NEWLINE)
 {
     assert('is_cstring($string) && is_int($width) && is_bitfield($wrappingFlags) && is_cstring($newline)', vs(isset($this), get_defined_vars()));
     assert('$width > 0', vs(isset($this), get_defined_vars()));
     // Constant. Newline character that is used by the input string (after newline normalization).
     $normNl = self::NEWLINE;
     // Constant. Determines what characters should be considered spaces.
     // A character in the "Zs" Unicode category, an HT, or a Zero Width Space, except No-break Space and Narrow
     // No-break Space.
     $spaceSubjectRe = "(\\p{Zs}|\\x{0009}|\\x{200B})(?<!\\x{00A0}|\\x{202F})";
     // Break enabling characters.
     // Soft Hyphen or Tibetan Mark Intersyllabic Tsheg.
     $breakAllowCharSubjectRe = "\\x{00AD}|\\x{0F0B}";
     // Retrieve the wrapping options.
     $breakSpacelessLines = CBitField::isBitSet($wrappingFlags, self::WRAPPING_BREAK_SPACELESS_LINES);
     $allowTrailingSpaces = CBitField::isBitSet($wrappingFlags, self::WRAPPING_ALLOW_TRAILING_SPACES);
     $disallowLeadingSpaces = CBitField::isBitSet($wrappingFlags, self::WRAPPING_DISALLOW_LEADING_SPACES);
     $dontBreakSpacelessCjkEndingLines = CBitField::isBitSet($wrappingFlags, self::WRAPPING_DONT_BREAK_SPACELESS_CJK_ENDING_LINES);
     // Normalize newlines in the input string.
     $string = self::normNewlines($string, $normNl);
     $normNlLength = self::length($normNl);
     $newString = "";
     $pos = 0;
     $bytePos = 0;
     $sLength = self::length($string);
     while (true) {
         $numCharsLeft = $sLength - $pos;
         // A portion begins at the very start or right after a newline, either it is native or added. The length of
         // a portion is the wrapping width or less.
         $portionLength = CMathi::min($width, $numCharsLeft);
         $portion = self::substr($string, $pos, $portionLength);
         $portionByteLength = CString::length($portion);
         if ($portionLength == $numCharsLeft) {
             // All done.
             $newString .= $portion;
             break;
         }
         // The starting position of the next portion.
         $nextPos = $pos + $portionLength;
         $nextBytePos = $bytePos + $portionByteLength;
         // Look for the first occurrence of a newline in the portion.
         $nlPos = self::indexOf($portion, $normNl);
         if ($nlPos != -1) {
             // This portion contains a newline, so the next portion is going to start right after this first found
             // newline.
             $subPLength = $nlPos + $normNlLength;
             $subP = self::substr($portion, 0, $subPLength);
             $newString .= $subP;
             $pos += $subPLength;
             $bytePos += CString::length($subP);
             continue;
         }
         // There are no newlines in this portion. Before the next step, make sure that the next portion is not
         // going to start with a newline.
         if ($numCharsLeft - $portionLength >= $normNlLength) {
             $nextPortionBeginning = self::substr($string, $nextPos, $normNlLength);
             if (self::indexOf($nextPortionBeginning, $normNl) == 0) {
                 // The next portion is going to start with a newline, so no need to break this one, regardless of
                 // whether or not it contains any spaces.
                 $newString .= $portion;
                 $pos = $nextPos;
                 $bytePos = $nextBytePos;
                 continue;
             }
         }
         // The next portion is not going to start with a newline. Look for the last occurrence of a space or
         // break-allow character in this portion.
         $lastSubjectBytePos = CRegex::lastIndexOf($portion, "/({$spaceSubjectRe})|({$breakAllowCharSubjectRe})/u", 0, $foundString);
         if ($lastSubjectBytePos != -1) {
             // Add a newline right after this last occurring space or break-allow character.
             $subP = CString::substring($portion, 0, $lastSubjectBytePos + CString::length($foundString));
             $newString .= $subP;
             $newString .= $newline;
             $pos += self::length($subP);
             $bytePos += CString::length($subP);
             continue;
         }
         // There are no spaces or break-allow characters in this portion. Consider adding a newline right after the
         // portion.
         if ($breakSpacelessLines || !$dontBreakSpacelessCjkEndingLines && self::hasCjkChar(self::charAt($portion, $portionLength - 1))) {
             $newString .= $portion;
             $newString .= $newline;
             $pos = $nextPos;
             $bytePos = $nextBytePos;
             continue;
         }
//.........这里部分代码省略.........
开发者ID:nunodotferreira,项目名称:Phred,代码行数:101,代码来源:CUString.php


注:本文中的CString::substring方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。