How to use substring method of StringUtils class

Best Gherkin-php code snippet using StringUtils.substring

StringUtils.php

Source:StringUtils.php Github

copy

Full Screen

...110 }111 } elseif ("\r" !== $lastChar) {112 ++$lastIndex;113 }114 return self::substring($str, 0, $lastIndex);115 }116 /**117 * Remove the specified last character from a `string`.118 *119 * If the `string` ends in `\r\n`, then remove both of them.120 *121 * StringUtils::chop(null); // null122 * StringUtils::chop(''); // ''123 * StringUtils::chop("abc \r"); // 'abc '124 * StringUtils::chop("abc\n"); // 'abc'125 * StringUtils::chop("abc\r\n"); // 'abc'126 * StringUtils::chop('abc'); // 'ab'127 * StringUtils::chop("abc\nabc"); // "abc\nab"128 * StringUtils::chop('a'); // ''129 * StringUtils::chop("\r"); // ''130 * StringUtils::chop("\n"); // ''131 * StringUtils::chop("\r\n"); // ''132 *133 * @param string $str The `string` to chop the last character from.134 *135 * @return string|null The `string` without the last character; `null` if136 * `null` `string` input.137 */138 public static function chop($str)139 {140 if (true === self::isEmpty($str)) {141 return $str;142 }143 if ("\r\n" === \substr($str, -2)) {144 return \substr($str, 0, -2);145 }146 return \substr($str, 0, -1);147 }148 /**149 * Checks if a `string` is whitespace, empty (`''`) or `null`.150 *151 * StringUtils::isBlank(null); // true152 * StringUtils::isBlank(''); // true153 * StringUtils::isBlank(' '); // true154 * StringUtils::isBlank('bob'); // false155 * StringUtils::isBlank(' bob '); // false156 *157 * @param string $str The `string` to check.158 *159 * @return boolean `true` if the `string` is `null`, empty or whitespace;160 * `false` otherwise.161 */162 public static function isBlank($str)163 {164 return self::EMPTY_STR === \trim($str);165 }166 /**167 * Checks if a `string` is not empty (`''`), not `null` and not whitespace168 * only.169 *170 * StringUtils::isNotBlank(null); // false171 * StringUtils::isNotBlank(''); // false172 * StringUtils::isNotBlank(' '); // false173 * StringUtils::isNotBlank('bob'); // true174 * StringUtils::isNotBlank(' bob '); // true175 *176 * @param string $str The `string` to check.177 *178 * @return boolean `true` if the `string` is not empty and not `null` and179 * not whitespace; `false` otherwise.180 */181 public static function isNotBlank($str)182 {183 return false === self::isBlank($str);184 }185 /**186 * Checks if a `string` is empty (`''`) or `null`.187 *188 * StringUtils::isEmpty(null); // true189 * StringUtils::isEmpty(''); // true190 * StringUtils::isEmpty(' '); // false191 * StringUtils::isEmpty('bob'); // false192 * StringUtils::isEmpty(' bob '); // false193 *194 * @param string $str The `string` to check.195 *196 * @return boolean `true` if the `string` is empty or `null`, `false`197 * otherwise.198 */199 public static function isEmpty($str)200 {201 return (self::EMPTY_STR === $str) || (null === $str);202 }203 /**204 * Checks if a `string` is not empty (`''`) and not `null`.205 *206 * StringUtils::isNotEmpty(null); // false207 * StringUtils::isNotEmpty(''); // false208 * StringUtils::isNotEmpty(' '); // true209 * StringUtils::isNotEmpty('bob'); // true210 * StringUtils::isNotEmpty(' bob '); // true211 *212 * @param string $str The `string` to check.213 *214 * @return boolean `true` if the `string` is not empty or `null`, `false`215 * otherwise.216 */217 public static function isNotEmpty($str)218 {219 return false === self::isEmpty($str);220 }221 /**222 * Returns the length of a `string` or `0` if the `string` is `null`.223 *224 * @param string $str The `string` to check.225 *226 * @return integer The length of the `string` or `0` if the `string` is227 * `null`.228 */229 public static function length($str)230 {231 return \strlen($str);232 }233 /**234 * Converts a `string` to lower case.235 *236 * A `null` input `string` returns `null`.237 *238 * StringUtils::lowerCase(null) = null239 * StringUtils::lowerCase('') = ''240 * StringUtils::lowerCase('aBc') = 'abc'241 *242 * @param string $str The `string` to lower case.243 *244 * @return string The lower cased `string` or `null` if `null` `string`245 * input.246 */247 public static function lowerCase($str)248 {249 return (true === self::isEmpty($str)) ? $str : strtolower($str);250 }251 /**252 * Converts a `string` to upper case.253 *254 * A `null` input `string` returns `null`.255 *256 * StringUtils::upperCase(null) = null257 * StringUtils::upperCase('') = ''258 * StringUtils::upperCase('aBc') = 'ABC'259 *260 * @param string $str The `string` to upper case.261 *262 * @return string The upper cased `string` or `null` if `null` `string`263 * input.264 */265 public static function upperCase($str)266 {267 return (true === self::isEmpty($str)) ? $str : strtoupper($str);268 }269 /**270 * Replaces a `string` with another `string` inside a larger `string`, for271 * the first maximum number of values to replace of the search `string`.272 *273 * StringUtils::replace(null, *, *, *) // null274 * StringUtils::replace('', *, *, *) // ''275 * StringUtils::replace('any', null, *, *) // 'any'276 * StringUtils::replace('any', *, null, *) // 'any'277 * StringUtils::replace('any', '', *, *) // 'any'278 * StringUtils::replace('any', *, *, 0) // 'any'279 * StringUtils::replace('abaa', 'a', null, -1) // 'abaa'280 * StringUtils::replace('abaa', 'a', '', -1) // 'b'281 * StringUtils::replace('abaa', 'a', 'z', 0) // 'abaa'282 * StringUtils::replace('abaa', 'a', 'z', 1) // 'zbaa'283 * StringUtils::replace('abaa', 'a', 'z', 2) // 'zbza'284 * StringUtils::replace('abaa', 'a', 'z', -1) // 'zbzz'285 *286 * @param string $text The `string` to search and replace in.287 * @param string $search The `string` to search for.288 * @param string $replace The `string` to replace $search with.289 * @param integer $max The maximum number of values to replace, or `-1`290 * if no maximum.291 *292 * @return string The text with any replacements processed or `null` if293 * `null` `string` input.294 */295 public static function replaceReg($text, $search, $replace, $max = -1)296 {297 if ((true === self::isEmpty($text)) || (true === self::isEmpty($search)) || (null === $replace) || (0 === $max)298 ) {299 return $text;300 }301 return \preg_replace(302 '/'.\preg_quote($search).'/', $replace, $text, $max303 );304 }305 /**306 * Replaces a `string` with another `string` inside a larger `string`, for307 * the first maximum number of values to replace of the search `string`.308 *309 * StringUtils::replace(null, *, *, *) // null310 * StringUtils::replace('', *, *, *) // ''311 * StringUtils::replace('any', null, *, *) // 'any'312 * StringUtils::replace('any', *, null, *) // 'any'313 * StringUtils::replace('any', '', *, *) // 'any'314 * StringUtils::replace('any', *, *, 0) // 'any'315 * StringUtils::replace('abaa', 'a', null, -1) // 'abaa'316 * StringUtils::replace('abaa', 'a', '', -1) // 'b'317 * StringUtils::replace('abaa', 'a', 'z', 0) // 'abaa'318 * StringUtils::replace('abaa', 'a', 'z', 1) // 'zbaa'319 * StringUtils::replace('abaa', 'a', 'z', 2) // 'zbza'320 * StringUtils::replace('abaa', 'a', 'z', -1) // 'zbzz'321 *322 * @param string $text The `string` to search and replace in.323 * @param string $search The `string` to search for.324 * @param string $replace The `string` to replace $search with.325 * @param integer $max The maximum number of values to replace, or `-1`326 * if no maximum.327 *328 * @return string The text with any replacements processed or `null` if329 * `null` `string` input.330 */331 public static function replace($text, $search, $replace, $max = null)332 {333 if ((true === self::isEmpty($text)) || (true === self::isEmpty($search)) || (null === $replace) || (0 === $max)334 ) {335 return $text;336 }337 return str_replace(338 $search, $replace, $text, $max339 );340 }341 /* -------------------------------------------------------------------------342 * Trim343 * ---------------------------------------------------------------------- */344 /**345 * Removes control characters (char &lt;= 32) from both ends of a `string`,346 * handling `null` by returning `null`.347 *348 * This method removes start and end characters &lt;= 32. To strip349 * whitespace use {@see strip}.350 *351 * To trim your choice of characters, use the {@see strip} method.352 *353 * StringUtils::trim(null); // null354 * StringUtils::trim(''); // ''355 * StringUtils::trim(' '); // ''356 * StringUtils::trim('abc'); // 'abc'357 * StringUtils::trim(' abc '); // 'abc'358 *359 * @param string $str The `string` to be trimmed.360 *361 * @return string The trimmed `string` or `null` if `null` `string` input.362 */363 public static function trim($str)364 {365 return (true === self::isEmpty($str)) ? $str : self::trimToEmpty($str);366 }367 /**368 * Removes control characters (char &lt;= 32) from both ends of a `string`369 * returning an empty `string` (`''`) if the `string` is empty (`''`) after370 * the trim or if it is `null`.371 *372 * This method removes start and end characters &lt;= 32. To strip373 * whitespace use {@see stripToEmpty}.374 *375 * StringUtils::trimToEmpty(null); // ''376 * StringUtils::trimToEmpty(''); // ''377 * StringUtils::trimToEmpty(' '); // ''378 * StringUtils::trimToEmpty('abc'); // 'abc'379 * StringUtils::trimToEmpty(' abc '); // 'abc'380 *381 * @param string $str The `string` to be trimmed.382 *383 * @return string The trimmed `string` or an empty `string` if `null` input.384 */385 public static function trimToEmpty($str)386 {387 return \trim($str);388 }389 /**390 * Removes control characters (char &lt;= 32) from both ends of a `string`391 * returning `null` if the `string` is empty (`''`) after the trim or if it392 * is `null`.393 *394 * This method removes start and end characters &lt;= 32. To strip395 * whitespace use {@see stripToNull}.396 *397 * StringUtils::trimToNull(null); // null398 * StringUtils::trimToNull(''); // null399 * StringUtils::trimToNull(' '); // null400 * StringUtils::trimToNull('abc'); // 'abc'401 * StringUtils::trimToNull(' abc '); // 'abc'402 *403 * @param string $str The `string` to be trimmed.404 *405 * @return string|null The trimmed `string' or `null` if only chars406 * &lt;= 32, empty or `null` `string` input.407 */408 public static function trimToNull($str)409 {410 $str = self::trimToEmpty($str);411 return (true === self::isEmpty($str)) ? null : $str;412 }413 /* -------------------------------------------------------------------------414 * Strip415 * ---------------------------------------------------------------------- */416 /**417 * Strips any of a set of characters from the start and end of a `string`.418 *419 * This is similar to {@see trim} but allows the characters to be stripped420 * to be controlled.421 *422 * A `null` input `string` returns `null`.423 * An empty string (`''`) input returns the empty `string`.424 *425 * If the `string` for the characters to remove is `null`, whitespace is426 * stripped.427 *428 * StringUtils::strip(null, *); // null429 * StringUtils::strip('', *); // ''430 * StringUtils::strip('abc', null); // 'abc'431 * StringUtils::strip(' abc', null); // 'abc'432 * StringUtils::strip('abc ', null); // 'abc'433 * StringUtils::strip(' abc ', null); // 'abc'434 * StringUtils::strip(' abcyx', 'xyz'); // ' abc'435 *436 * @param string $str The `string` to remove characters from.437 * @param string $chars The characters to remove. `null` is treated as438 * whitespace.439 *440 * @return string|null The stripped `string` or `null` if `null` `string`441 * input.442 */443 public static function strip($str, $chars)444 {445 return (true === self::isEmpty($str)) ? $str : self::stripEnd(self::stripStart($str, $chars), $chars);446 }447 /**448 * Strips whitespace from the start and end of a `string` returning an empty449 * `string` if `null` input.450 *451 * This is similar to {@see trimToEmpty} but removes whitespace.452 *453 * StringUtils::stripToEmpty(null); // ''454 * StringUtils::stripToEmpty(''); // ''455 * StringUtils::stripToEmpty(' '); // ''456 * StringUtils::stripToEmpty('abc'); // 'abc'457 * StringUtils::stripToEmpty(' abc'); // 'abc'458 * StringUtils::stripToEmpty('abc '); // 'abc'459 * StringUtils::stripToEmpty(' abc '); // 'abc'460 * StringUtils::stripToEmpty(' ab c '); // 'ab c'461 *462 * @param string $str The `string` to be stripped.463 *464 * @return string The stripped `string` or an empty `string` if `null`465 * input.466 */467 public static function stripToEmpty($str)468 {469 return (null === $str) ? self::EMPTY_STR : self::strip($str, null);470 }471 /**472 * Strips whitespace from the start and end of a `string` returning `null`473 * if the `string` is empty (`''`) after the strip.474 *475 * This is similar to {@see trimToNull} but removes whitespace.476 *477 * StringUtils::stripToNull(null); // null478 * StringUtils::stripToNull(''); // null479 * StringUtils::stripToNull(' '); // null480 * StringUtils::stripToNull('abc'); // 'abc'481 * StringUtils::stripToNull(' abc'); // 'abc'482 * StringUtils::stripToNull('abc '); // 'abc'483 * StringUtils::stripToNull(' abc '); // 'abc'484 * StringUtils::stripToNull(' ab c '); // 'ab c'485 *486 * @param string $str The `string` to be stripped.487 *488 * @return string|null The stripped `string` or `null` if whitespace, empty489 * or `null` `string` input.490 */491 public static function stripToNull($str)492 {493 $str = self::strip($str, null);494 return (0 === self::length($str)) ? null : $str;495 }496 /**497 * Strips any of a set of characters from the start of a `string`.498 *499 * A `null` input `string` returns `null`.500 * An empty string (`''`) input returns the empty `string`.501 *502 * If the `string` for the characters to remove is `null`, whitespace is503 * stripped.504 *505 * StringUtils::stripStart(null, *); // null506 * StringUtils::stripStart('', *); // ''507 * StringUtils::stripStart('abc', ''); // 'abc'508 * StringUtils::stripStart('abc', null); // 'abc'509 * StringUtils::stripStart(' abc', null); // 'abc'510 * StringUtils::stripStart('abc ', null); // 'abc '511 * StringUtils::stripStart(' abc ', null); // 'abc '512 * StringUtils::stripStart('yxabc ', 'xyz'); // 'abc '513 *514 * @param string $str The `string` to remove characters from.515 * @param string $chars The characters to remove. `null` is treated as516 * whitespace.517 *518 * @return string|null The stripped `string` or `null` if `null` `string`519 * input.520 */521 public static function stripStart($str, $chars)522 {523 if (true === self::isEmpty($str)) {524 return $str;525 }526 return (null === $chars) ? \ltrim($str) : \ltrim($str, $chars);527 }528 /**529 * Strips any of a set of characters from the end of a `string`.530 *531 * A `null` input `string` returns `null`.532 * An empty string (`''`) input returns the empty `string`.533 *534 * If the `string` for the characters to remove is `null`, whitespace is535 * stripped.536 *537 * StringUtils::stripEnd(null, *) = null538 * StringUtils::stripEnd('', *) = ''539 * StringUtils::stripEnd('abc', '') = 'abc'540 * StringUtils::stripEnd('abc', null) = 'abc'541 * StringUtils::stripEnd(' abc', null) = ' abc'542 * StringUtils::stripEnd('abc ', null) = 'abc'543 * StringUtils::stripEnd(' abc ', null) = ' abc'544 * StringUtils::stripEnd(' abcyx', 'xyz') = ' abc'545 *546 * @param string $str The `string` to remove characters from.547 * @param string $chars The characters to remove. `null` is treated as548 * whitespace.549 *550 * @return string|null The stripped `string` or `null` if `null` `string`551 * input.552 */553 public static function stripEnd($str, $chars)554 {555 if (true === self::isEmpty($str)) {556 return $str;557 }558 return (null === $chars) ? \rtrim($str) : \rtrim($str, $chars);559 }560 /* -------------------------------------------------------------------------561 * Compare562 * ---------------------------------------------------------------------- */563 /**564 * Compares two `string`s lexicographically.565 *566 * The comparison is based on the Unicode value of each character in the567 * `string`s. The character sequence represented by the first `string` is568 * compared lexicographically to the character sequence represented by the569 * second `string`. The result is a negative integer if the first `string`570 * lexicographically precedes the second `string`. The result is a positive571 * integer if the first `string` lexicographically follows the second572 * `string`.573 *574 * This method returns an integer whose sign is that of calling {@see575 * compare} with normalized versions of the `string`s.576 *577 * @param string $str1 The first `string` to be compared.578 * @param string $str2 The second `string` to be compared.579 *580 * @return integer A negative integer, zero, or a positive integer as the581 * first `string` is less than, equal to, or greater than the second582 * `string`.583 */584 public static function compare($str1, $str2)585 {586 return \strcmp($str1, $str2);587 }588 /**589 * Compares two `string`s lexicographically, ignoring case differences.590 *591 * This method returns an integer whose sign is that of calling {@see592 * compare} with normalized versions of the `string`s.593 *594 * @param string $str1 The first `string` to be compared.595 * @param string $str2 The second `string` to be compared.596 *597 * @return integer A negative integer, zero, or a positive integer as the598 * first `string` is greater than, equal to, or less than the second599 * `string`, ignoring case considerations.600 */601 public static function compareIgnoreCase($str1, $str2)602 {603 return \strcasecmp($str2, $str1);604 }605 /* -------------------------------------------------------------------------606 * Equals607 * ---------------------------------------------------------------------- */608 /**609 * Compares two `string`s, returning `true` if they are equal.610 *611 * `null`s are handled without exceptions. Two `null` references are612 * considered to be equal. The comparison is case sensitive.613 *614 * StringUtils::equals(null, null); // true615 * StringUtils::equals(null, 'abc'); // false616 * StringUtils::equals('abc', null); // false617 * StringUtils::equals('abc', 'abc'); // true618 * StringUtils::equals('abc', 'ABC'); // false619 *620 * @param string $str1 The first `string`.621 * @param string $str2 The second `string`.622 *623 * @return boolean `true` if the `string`s are equal, case sensitive, or624 * both `null`.625 */626 public static function equal($str1, $str2)627 {628 return (null === $str1) ? (null === $str2) : ($str1 === $str2);629 }630 /**631 * Compares two `string`s, returning `true` if they are equal ignoring the632 * case.633 *634 * `null`s are handled without exceptions. Two `null` references are635 * considered to be equal. The comparison is case insensitive.636 *637 * StringUtils::equalsIgnoreCase(null, null); // true638 * StringUtils::equalsIgnoreCase(null, 'abc'); // false639 * StringUtils::equalsIgnoreCase('abc', null); // false640 * StringUtils::equalsIgnoreCase('abc', 'abc'); // true641 * StringUtils::equalsIgnoreCase('abc', 'ABC'); // true642 *643 * @param string $str1 The first `string`.644 * @param string $str2 The second `string`.645 *646 * @return boolean `true` if the `string`s are equal, case insensitive, or647 * both `null`.648 */649 public static function equalsIgnoreCase($str1, $str2)650 {651 return (null === $str1) ? (null === $str2) : (self::lowercase($str1) === self::lowercase($str2));652 }653 /**654 * Finds the first index within a `string` from a start position, handling655 * `null`.656 *657 * A `null` or empty (`''`) `string` will return `-1`.658 * A negative start position is treated as zero.659 * A start position greater than the string length returns `-1`.660 *661 * StringUtils::indexOf(null, *, *); // -1662 * StringUtils::indexOf(*, null, *); // -1663 * StringUtils::indexOf('', '', 0); // 0664 * StringUtils::indexOf('aabaabaa', 'a', 0); // 0665 * StringUtils::indexOf('aabaabaa', 'b', 0); // 2666 * StringUtils::indexOf('aabaabaa', 'ab', 0); // 1667 * StringUtils::indexOf('aabaabaa', 'b', 3); // 5668 * StringUtils::indexOf('aabaabaa', 'b', 9); // -1669 * StringUtils::indexOf('aabaabaa', 'b', -1); // 2670 * StringUtils::indexOf('aabaabaa', '', 2); // 2671 * StringUtils::indexOf('abc', '', 9); // 3672 *673 * @param string $str The `string` to check.674 * @param string $search The `string` to find.675 * @param integer $startPos The start position, negative treated as zero.676 *677 * @return integer The first index of the search character, `-1` if no match678 * or `null` `string` input.679 */680 public static function indexOf($str, $search, $startPos = 0)681 {682 $result = self::validateIndexOf($str, $search, $startPos);683 if (true !== $result) {684 return $result;685 }686 if (true === self::isEmpty($search)) {687 return $startPos;688 }689 $pos = \strpos($str, $search, $startPos);690 return (false === $pos) ? -1 : $pos;691 }692 /**693 * Helper method for {@see indexOf} and {@see lastIndexOf}.694 *695 * @param string $str The `string` to check.696 * @param string $search The `string` to find.697 * @param integer $startPos The start position, negative treated as zero.698 *699 * @return integer|boolean `-1` if no match or `null` `string` input; `true`700 * otherwise.701 */702 private static function validateIndexOf($str, $search, &$startPos)703 {704 if ((null === $str) || (null === $search)) {705 return -1;706 }707 $lengthSearch = self::length($search);708 $lengthStr = self::length($str);709 if ((0 === $lengthSearch) && ($startPos >= $lengthStr)) {710 return $lengthStr;711 }712 if ($startPos >= $lengthStr) {713 return -1;714 }715 if (0 > $startPos) {716 $startPos = 0;717 }718 return true;719 }720 /**721 * Finds the first index within a `string`, handling `null`.722 *723 * A `null` `string` will return `-1`.724 * A negative start position returns `-1`. An empty (`''`) search `string`725 * always matches unless the start position is negative.726 * A start position greater than the `string` length searches the whole727 * `string`.728 *729 * StringUtils::lastIndexOf(null, *, *); // -1730 * StringUtils::lastIndexOf(*, null, *); // -1731 * StringUtils::lastIndexOf('aabaabaa', 'a', 8); // 7732 * StringUtils::lastIndexOf('aabaabaa', 'b', 8); // 5733 * StringUtils::lastIndexOf('aabaabaa', 'ab', 8); // 4734 * StringUtils::lastIndexOf('aabaabaa', 'b', 9); // 5735 * StringUtils::lastIndexOf('aabaabaa', 'b', -1); // -1736 * StringUtils::lastIndexOf('aabaabaa', 'a', 0); // 0737 * StringUtils::lastIndexOf('aabaabaa', 'b', 0); // -1738 *739 * @param string $str The `string` to check.740 * @param string $search The `string` to find.741 * @param integer $startPos The start position, negative treated as zero.742 *743 * @return integer The first index of the search `string`, `-1` if no match744 * or null `string` input.745 */746 public static function lastIndexOf($str, $search, $startPos = 0)747 {748 $result = self::validateIndexOf($str, $search, $startPos);749 if (true !== $result) {750 return $result;751 }752 if (true === self::isEmpty($search)) {753 return $startPos;754 }755 $pos = \strrpos($str, $search, $startPos);756 return (false === $pos) ? -1 : $pos;757 }758 /* -------------------------------------------------------------------------759 * Split760 * ---------------------------------------------------------------------- */761 /**762 * Splits the provided text into an `array` with a maximum length,763 * separators specified.764 *765 * The separator is not included in the returned `string` `array`. Adjacent766 * separators are treated as one separator. A `null` input `string` returns767 * `null`. A `null` $chars splits on whitespace. If more than $max768 * delimited substrings are found, the returned `string` includes all769 * characters after the first `$max - 1` returned `string`s (including770 * separator characters).771 *772 * StringUtils::split(null, null, null); // null773 * StringUtils::split('', null, null); // []774 * StringUtils::split('ab cd ef', null, 0); // ['ab', 'cd', 'ef']775 * StringUtils::split('ab cd ef', null, 0); // ['ab', 'cd', 'ef']776 * StringUtils::split('ab:cd:ef', ':', 0); // ['ab', 'cd', 'ef']777 * StringUtils::split('ab:cd:ef', ':', 2); // ['ab', 'cd:ef']778 *779 * @param string $str The `string` to parse.780 * @param string $chars The characters used as the delimiters, `null`781 * splits on whitespace.782 * @param integer $max The maximum number of elements to include in the783 * `array.` A zero or negative value implies no limit.784 *785 * @return array|null An `array` of parsed `string`s, `null` if `null`786 * `string` input.787 */788 public static function split($str, $chars = null, $max = 0)789 {790 $result = self::EMPTY_STR;791 if (null === $str) {792 return null;793 }794 if (self::EMPTY_STR === $str) {795 return array();796 }797 if (null === $chars) {798 $result = \preg_split('/\s+/', $str, $max);799 } elseif ($max > 0) {800 $result = \explode($chars, $str, $max);801 } else {802 $result = \explode($chars, $str);803 }804 return $result;805 }806 /**807 * Gets a substring from the specified `string` avoiding exceptions.808 *809 * A negative start position can be used to start/end *n* characters from810 * the end of the `string`.811 *812 * The returned substring starts with the character in the `$start` position813 * and ends before the `$end` position. All position counting is zero-based814 * -- i.e., to start at the beginning of the `string` use `$start = 0`.815 *816 * Negative start and end positions can be used to specify offsets relative817 * to the end of the `string`.818 *819 * If `$start` is not strictly to the left of `$end`, the empty string is820 * returned.821 *822 * StringUtils::substring(null, *); // null823 * StringUtils::substring('', *); // ''824 * StringUtils::substring('abc', 0); // 'abc'825 * StringUtils::substring('abc', 2); // 'c'826 * StringUtils::substring('abc', 4); // ''827 * StringUtils::substring('abc', -2); // 'bc'828 * StringUtils::substring('abc', -4); // 'abc'829 * StringUtils::substring(null, *, *); // null830 * StringUtils::substring('', * , *); // '';831 * StringUtils::substring('abc', 0, 2); // 'ab'832 * StringUtils::substring('abc', 2, 0); // ''833 * StringUtils::substring('abc', 2, 4); // 'c'834 * StringUtils::substring('abc', 4, 6); // ''835 * StringUtils::substring('abc', 2, 2); // ''836 * StringUtils::substring('abc', -2, -1); // 'b'837 * StringUtils::substring('abc', -4, 2); // 'ab'838 *839 * @param string $str The `string` to get the substring from.840 * @param integer $start The position to start from, negative means count841 * back from the end of the `string` by this many characters.842 * @param integer $end The position to end at (exclusive), negative means843 * count back from the end of the `string` by this many characters.844 *845 * @return string|null The substring from start position to end position,846 * `null` if `null` `string` input.847 */848 public static function substring($str, $start, $end = null)849 {850 if ((0 > $start) && (0 < $end)) {851 $start = 0;852 }853 if (null === $end) {854 $end = self::length($str);855 }856 return \substr($str, $start, $end - $start);857 }858 /**859 * Gets the substring after the first occurrence of a separator.860 *861 * The separator is not returned.862 *863 * A `null` `string` input will return `null`.864 * An empty (`''`) `string` input will return the empty `string`.865 * A `null` separator will return the empty `string` if the input `string`866 * is not `null`.867 *868 * If nothing is found, the empty `string` is returned.869 *870 * StringUtils::substringAfter(null, *); // null871 * StringUtils::substringAfter('', *); // ''872 * StringUtils::substringAfter(*, null); // ''873 * StringUtils::substringAfter('abc', 'a'); // 'bc'874 * StringUtils::substringAfter('abcba', 'b'); // 'cba'875 * StringUtils::substringAfter('abc', 'c'); // ''876 * StringUtils::substringAfter('abc', 'd'); // ''877 * StringUtils::substringAfter('abc', ''); // 'abc'878 *879 * @param string $str The `string` to get a substring from.880 * @param string $separator The `string` to search for.881 *882 * @return string|null The substring after the first occurrence of the883 * separator, `null` if `null` `string` input.884 */885 public static function substringAfter($str, $separator)886 {887 if (true === self::isEmpty($str)) {888 return $str;889 }890 if (null === $separator) {891 return self::EMPTY_STR;892 }893 $pos = self::indexOf($str, $separator);894 if (self::INDEX_NOT_FOUND === $pos) {895 return self::EMPTY_STR;896 }897 return self::substring($str, $pos + self::length($separator));898 }899 /**900 * Gets the substring after the last occurrence of a separator.901 *902 * The separator is not returned.903 *904 * A `null` `string` input will return `null`.905 * An empty (`''`) `string` input will return the empty `string`.906 * An empty or `null` separator will return the empty `string` if the input907 * `string` is not `null`.908 *909 * If nothing is found, the empty `string` is returned.910 *911 * StringUtils::substringAfterLast(null, *); // null912 * StringUtils::substringAfterLast('', *); // ''913 * StringUtils::substringAfterLast(*, ''); // ''914 * StringUtils::substringAfterLast(*, null); // ''915 * StringUtils::substringAfterLast('abc', 'a'); // 'bc'916 * StringUtils::substringAfterLast('abcba', 'b'); // 'a'917 * StringUtils::substringAfterLast('abc', 'c'); // ''918 * StringUtils::substringAfterLast('a', 'a'); // ''919 * StringUtils::substringAfterLast('a', 'z'); // ''920 *921 * @param string $str The `string` to get a substring from.922 * @param string $separator The `string` to search for.923 *924 * @return string|null The substring after the last occurrence of the925 * separator, `null` if `null` `string` input.926 */927 public static function substringAfterLast($str, $separator)928 {929 if (true === self::isEmpty($str)) {930 return $str;931 }932 if (true === self::isEmpty($separator)) {933 return self::EMPTY_STR;934 }935 $pos = self::lastIndexOf($str, $separator);936 if (self::INDEX_NOT_FOUND === $pos || (self::length($str) - self::length($separator)) === $pos937 ) {938 return self::EMPTY_STR;939 }940 return self::substring($str, $pos + self::length($separator));941 }942 /**943 * Gets the substring before the first occurrence of a separator.944 *945 * The separator is not returned.946 *947 * A `null` `string` input will return `null`.948 * An empty (`''`) `string` input will return the empty `string`.949 * A `null` separator will return the input string.950 *951 * If nothing is found, the `string` input is returned.952 *953 * StringUtils::substringBefore(null, *); // null954 * StringUtils::substringBefore('', *); // ''955 * StringUtils::substringBefore('abc', 'a'); // ''956 * StringUtils::substringBefore('abcba', 'b'); // 'a'957 * StringUtils::substringBefore('abc', 'c'); // 'ab'958 * StringUtils::substringBefore('abc', 'd'); // 'abc'959 * StringUtils::substringBefore('abc', ''); // ''960 * StringUtils::substringBefore('abc', null); // 'abc'961 *962 * @param string $str The `string` to get a substring from.963 * @param string $separator The `string` to search for.964 *965 * @return string|null The substring before the first occurrence of the966 * separator, `null` if `null` `string` input.967 */968 public static function substringBefore($str, $separator)969 {970 if ((true === self::isEmpty($str)) || (null === $separator)) {971 return $str;972 }973 if (0 === self::length($separator)) {974 return self::EMPTY_STR;975 }976 $pos = self::indexOf($str, $separator);977 if (self::INDEX_NOT_FOUND === $pos) {978 return $str;979 }980 return self::substring($str, 0, $pos);981 }982 /**983 * Gets the substring before the last occurrence of a separator.984 *985 * The separator is not returned.986 *987 * A `null` `string` input will return `null`.988 * An empty (`''`) `string` input will return the empty `string`.989 * An empty or `null` separator will return the input `string`.990 *991 * If nothing is found, the `string` input is returned.992 *993 * StringUtils::substringBeforeLast(null, *); // null994 * StringUtils::substringBeforeLast('', *); // ''995 * StringUtils::substringBeforeLast('abcba', 'b'); // 'abc'996 * StringUtils::substringBeforeLast('abc', 'c'); // 'ab'997 * StringUtils::substringBeforeLast('a', 'a'); // ''998 * StringUtils::substringBeforeLast('a', 'z'); // 'a'999 * StringUtils::substringBeforeLast('a', null); // 'a'1000 * StringUtils::substringBeforeLast('a', ''); // 'a'1001 *1002 * @param string $str The `string` to get a substring from.1003 * @param string $separator The `string` to search for.1004 *1005 * @return string|null The substring before the last occurrence of the1006 * seperator, `null` if `null` `string` input.1007 */1008 public static function substringBeforeLast($str, $separator)1009 {1010 if ((true === self::isEmpty($str)) || (true === self::isEmpty($separator))1011 ) {1012 return $str;1013 }1014 $pos = self::lastIndexOf($str, $separator);1015 if (self::INDEX_NOT_FOUND === $pos) {1016 return $str;1017 }1018 return self::substring($str, 0, $pos);1019 }1020 /**1021 * Gets the `string` that is nested in between two `string`s.1022 *1023 * Only the first match is returned.1024 *1025 * A `null` input `string` returns `null`. A `null` `$open`/`$close` returns1026 * `null` (no match). An empty (`''`) `$open` and `$close` returns an empty1027 * `string`.1028 *1029 * StringUtils::substringBetween('wx[b]yz', '[', ']'); // 'b'1030 * StringUtils::substringBetween(null, *, *); // null1031 * StringUtils::substringBetween(*, null, *); // null1032 * StringUtils::substringBetween(*, *, null); // null1033 * StringUtils::substringBetween('', '', ''); // ''1034 * StringUtils::substringBetween('', '', ']'); // null1035 * StringUtils::substringBetween('', '[', ']'); // null1036 * StringUtils::substringBetween('yabcz', '', ''); // ''1037 * StringUtils::substringBetween('yabcz', 'y', 'z'); // 'abc'1038 * StringUtils::substringBetween('yabczyabcz', 'y', 'z'); // 'abc'1039 *1040 * @param string $str The `string` containing the substrings, `null`1041 * returns `null`, empty returns empty.1042 * @param string $open The `string` identifying the start of the substring,1043 * empty returns `null`.1044 * @param string $close The `string` identifying the end of the substring,1045 * empty returns `null`.1046 *1047 * @return string|null The `string` after the substring, `null` if no match.1048 */1049 public static function substringBetween($str, $open, $close = null)1050 {1051 $result = null;1052 if (null === $close) {1053 $close = $open;1054 }1055 $startPos = self::indexOf($str, $open);1056 if (self::INDEX_NOT_FOUND !== $startPos) {1057 $startPos += self::length($open);1058 $endPos = self::indexOf($str, $close, $startPos);1059 if (self::INDEX_NOT_FOUND !== $endPos) {1060 $result = self::substring($str, $startPos, $endPos);1061 }1062 }1063 return $result;1064 }1065 /* -------------------------------------------------------------------------1066 * Capitalizing1067 * ---------------------------------------------------------------------- */1068 /**1069 * Capitalizes a `string` changing the first letter to upper case.1070 *1071 * No other letters are changed. For a word based algorithm, see {@see1072 * capitalize}. A `null` input `string` returns `null`.1073 *1074 * StringUtils::capitalize(null); // null1075 * StringUtils::capitalize(''); // ''1076 * StringUtils::capitalize('cat'); // 'Cat'1077 * StringUtils::capitalize('cAt'); // 'CAt'1078 *1079 * @param string $str The `string` to capitalize.1080 *1081 * @return string|null The capitalized `string` or `null` if `null` `string`1082 * input.1083 * @see StringUtils::uncapitalize, WordUtils::capitalize1084 */1085 public static function capitalize($str)1086 {1087 return \ucfirst($str);1088 }1089 /**1090 * Uncapitalizes a `string` changing the first letter to lower case.1091 *1092 * No other letters are changed. For a word based algorithm, see {@see1093 * uncapitalize}. A `null` input `string` returns `null`.1094 *1095 * StringUtils::uncapitalize(null); // null1096 * StringUtils::uncapitalize(''); // ''1097 * StringUtils::uncapitalize('Cat'); // 'cat'1098 * StringUtils::uncapitalize('CAT'); // 'cAT'1099 *1100 * @param string $str The `string` to uncapitalize.1101 *1102 * @return string|null The uncapitalized `string` or `null` if `null`1103 * `string` input.1104 * @see StringUtils::capitalize, WordUtils::uncapitalize1105 */1106 public static function uncapitalize($str)1107 {1108 return \lcfirst($str);1109 }1110 /* -------------------------------------------------------------------------1111 * Repeating1112 * ---------------------------------------------------------------------- */1113 /**1114 * Repeats a `string` the specified number of times to form a new `string`,1115 * with a specified `string` injected each time.1116 *1117 * StringUtils::repeat(null, 2, null); // null1118 * StringUtils::repeat(null, 2, 'x'); // null1119 * StringUtils::repeat('', 0, null); // ''1120 * StringUtils::repeat('', 2, ''); // ''1121 * StringUtils::repeat('', 3, 'x'); // 'xxx'1122 * StringUtils::repeat('?', 3, ', '); // '?, ?, ?'1123 *1124 * @param string $str The `string` to repeat.1125 * @param integer $repeat The number of times to repeat $str, negative1126 * treated as zero.1127 * @param string $separator The `string` to inject.1128 *1129 * @return string|null The capitalized `string` or `null` if `null` `string`1130 * input.1131 */1132 public static function repeat($str, $repeat, $separator = null)1133 {1134 $result = self::EMPTY_STR;1135 if ((null === $str) || (null === $separator)) {1136 $result = \str_repeat($str, $repeat);1137 } else {1138 $result = \str_repeat($str.$separator, $repeat);1139 if (true === self::isNotEmpty($str)) {1140 $result = self::removeEnd($result, $separator);1141 }1142 }1143 return $result;1144 }1145 /* -------------------------------------------------------------------------1146 * Remove1147 * ---------------------------------------------------------------------- */1148 /**1149 * Checks if a `string` ends with a specified suffix.1150 *1151 * `null`s are handled without exceptions. Two `null` references are1152 * considered to be equal. The comparison is case sensitive.1153 *1154 * StringUtils::endsWith(null, null); // true1155 * StringUtils::endsWith(null, 'def'); // false1156 * StringUtils::endsWith('abcdef', null); // false1157 * StringUtils::endsWith('abcdef', 'def'); // true1158 * StringUtils::endsWith('ABCDEF', 'def'); // false1159 * StringUtils::endsWith('ABCDEF', 'cde'); // false1160 *1161 * @param string $str The `string` to check.1162 * @param string $suffix The suffix to find.1163 *1164 * @return boolean `true` if the `string` $str ends with the suffix $suffix,1165 * case sensitive, or both `null`.1166 */1167 public static function endsWith($str, $suffix)1168 {1169 return ((null === $str) && (null === $suffix)) ? true : self::substring(1170 $str, self::length($str) - self::length($suffix)1171 ) === $suffix;1172 }1173 /**1174 * Checks if a `string` starts with a specified prefix.1175 *1176 * `null`s are handled without exceptions. Two `null` references are1177 * considered to be equal. The comparison is case sensitive.1178 *1179 * StringUtils::startsWith(null, null); // true1180 * StringUtils::startsWith(null, 'abc'); // false1181 * StringUtils::startsWith('abcdef', null); // false1182 * StringUtils::startsWith('abcdef', 'abc'); // true1183 * StringUtils::startsWith('ABCDEF', 'abc'); // false1184 *1185 * @param string $str The `string` to check.1186 * @param string $prefix The prefix to find.1187 *1188 * @return boolean `true` if the `string` `$str` starts with the prefix1189 * `$prefix`, case sensitive, or both `null`.1190 */1191 public static function startsWith($str, $prefix)1192 {1193 return ((null === $str) && (null === $prefix)) ? true : self::substring($str, 0, self::length($prefix)) === $prefix;1194 }1195 /* -------------------------------------------------------------------------1196 * Remove1197 * ---------------------------------------------------------------------- */1198 /**1199 * Removes a substring only if it is at the end of a source `string`,1200 * otherwise returns the source `string`.1201 *1202 * A `null` source `string` will return `null`.1203 * An empty (`''`) source `string` will return the empty `string`.1204 * A `null` search `string` will return the source `string`.1205 *1206 * StringUtils::removeEnd(null, *); // null1207 * StringUtils::removeEnd('', *); // ''1208 * StringUtils::removeEnd(*, null); // *1209 * StringUtils::removeEnd('www.domain.com', '.com.'); // 'www.domain.com'1210 * StringUtils::removeEnd('www.domain.com', '.com'); // 'www.domain'1211 * StringUtils::removeEnd('www.domain.com', 'domain'); // 'www.domain.com'1212 * StringUtils::removeEnd('abc', ''); // 'abc'1213 *1214 * @param string $str The source `string` to search.1215 * @param string $remove The `string` to search for and remove.1216 *1217 * @return string|null The substring with the `string` removed if found,1218 * `null` if `null` `string` input.1219 */1220 public static function removeEnd($str, $remove)1221 {1222 if ((true === self::isEmpty($str)) || (true === self::isEmpty($remove))1223 ) {1224 return $str;1225 }1226 if (true === self::endsWith($str, $remove)) {1227 return self::substring(1228 $str, 0, self::length($str) - self::length($remove)1229 );1230 }1231 return $str;1232 }1233 /**1234 * Removes a substring only if it is at the beginning of a source `string`,1235 * otherwise returns the source `string`.1236 *1237 * A `null` source string will return `null`.1238 * An empty (`''`) source `string` will return the empty `string`.1239 * A `null` search `string` will return the source `string`.1240 *1241 * StringUtils::removeStart(null, *); // null1242 * StringUtils::removeStart('', *); // ''1243 * StringUtils::removeStart(*, null); // *1244 * StringUtils::removeStart('www.domain.com', 'www.'); // 'domain.com'1245 * StringUtils::removeStart('domain.com', 'www.'); // 'domain.com'1246 * StringUtils::removeStart('www.domain.com', 'domain'); // 'www.domain.com'1247 * StringUtils::removeStart('abc', ''); // 'abc'1248 *1249 * @param string $str The source `string` to search.1250 * @param string $remove The `string` to search for and remove.1251 *1252 * @return string|null The substring with the `string` removed if found,1253 * `null` if `null` `string` input.1254 */1255 public static function removeStart($str, $remove)1256 {1257 if ((true === self::isEmpty($str)) || (true === self::isEmpty($remove))1258 ) {1259 return $str;1260 }1261 if (true === self::startsWith($str, $remove)) {1262 return self::substring($str, self::length($remove));1263 }1264 return $str;1265 }1266 /**1267 * @param $needle1268 * @param $haystack1269 * @return bool1270 */1271 public static function contains($needle, $haystack)1272 {1273 return strpos($haystack, $needle) !== false;1274 }1275 /**1276 * @param $str...

Full Screen

Full Screen

StringUtilsTest.php

Source:StringUtilsTest.php Github

copy

Full Screen

...810 }811 /**812 * @return void813 *814 * @coversDefaultClass substring815 * @dataProvider providerSubstring816 * @test817 */818 public function testSubstring($expected, $str, $start, $end = null)819 {820 $actual = StringUtils::substring($str, $start, $end);821 $this->assertEquals($expected, $actual);822 }823 /**824 * @return array825 */826 public function providerSubstringAfter()827 {828 return array(829 array(null, null, null),830 array('', '', null),831 array('', null, null),832 array('', 'abc', null),833 array('bc', 'abc', 'a'),834 array('cba', 'abcba', 'b'),835 array('', 'abc', 'c'),836 array('', 'abc', 'd'),837 array('abc', 'abc', '')838 );839 }840 /**841 * @return void842 *843 * @coversDefaultClass substringAfter844 * @dataProvider providerSubstringAfter845 * @test846 */847 public function testSubstringAfter($expected, $str, $separator)848 {849 $actual = StringUtils::substringAfter($str, $separator);850 $this->assertEquals($expected, $actual);851 }852 /**853 * @return array854 */855 public function providerSubstringAfterLast()856 {857 return array(858 array(null, null, null),859 array('', '', null),860 array('', null, ''),861 array('', 'abc', null),862 array('', null, null),863 array('bc', 'abc', 'a'),864 array('a', 'abcba', 'b'),865 array('', 'abc', 'c'),866 array('', 'a', 'a'),867 array('', 'a', 'z')868 );869 }870 /**871 * @return void872 *873 * @coversDefaultClass substringAfterLast874 * @dataProvider providerSubstringAfterLast875 * @test876 */877 public function testSubstringAfterLast($expected, $str, $separator)878 {879 $actual = StringUtils::substringAfterLast($str, $separator);880 $this->assertEquals($expected, $actual);881 }882 /**883 * @return array884 */885 public function providerSubstringBefore()886 {887 return array(888 array(null, null, null),889 array('', '', null),890 array('', 'abc', 'a'),891 array('a', 'abcba', 'b'),892 array('ab', 'abc', 'c'),893 array('abc', 'abc', 'd'),894 array('', 'abc', ''),895 array('abc', 'abc', null)896 );897 }898 /**899 * @return void900 *901 * @coversDefaultClass substringBefore902 * @dataProvider providerSubstringBefore903 * @test904 */905 public function testSubstringBefore($expected, $str, $separator)906 {907 $actual = StringUtils::substringBefore($str, $separator);908 $this->assertEquals($expected, $actual);909 }910 /**911 * @return array912 */913 public function providerSubstringBeforeLast()914 {915 return array(916 array(null, null, null),917 array('', '', null),918 array('abc', 'abcba', 'b'),919 array('ab', 'abc', 'c'),920 array('', 'a', 'a'),921 array('a', 'a', 'z'),922 array('a', 'a', null),923 array('a', 'a', '')924 );925 }926 /**927 * @return void928 *929 * @coversDefaultClass substringBeforeLast930 * @dataProvider providerSubstringBeforeLast931 * @test932 */933 public function testSubstringBeforeLast($expected, $str, $separator)934 {935 $actual = StringUtils::substringBeforeLast($str, $separator);936 $this->assertEquals($expected, $actual);937 }938 /**939 * @return array940 */941 public function providerSubstringBetween()942 {943 return array(944 array(null, null, 'tag'),945 array('','', ''),946 array(null, '', 'abc'),947 array('', ' ', ' '),948 array(null, 'abc', null),949 array('', 'abc', ''),950 array(null, 'abc', 'a'),951 array('bc', 'abca', 'a'),952 array('bc', 'abcabca', 'a'),953 array('bar', '\nbar\n', '\n'),954 array(null, null, '', ''),955 array(null, '', null, ''),956 array(null, '', '', null),957 array('', '', '', ''),958 array('', 'foo', '', ''),959 array(null, 'foo', '', ']'),960 array(null, 'foo', '[', ']'),961 array('', ' ', ' ', ' '),962 array('bar', '<foo>bar</foo>', '<foo>', '</foo>')963 );964 }965 /**966 * @return void967 *968 * @coversDefaultClass substringBetween969 * @dataProvider providerSubstringBetween970 * @test971 */972 public function testSubstringBetween($expected, $str, $open, $close = null)973 {974 $actual = StringUtils::substringBetween($str, $open, $close);975 $this->assertEquals($expected, $actual);976 }977 /**978 * @return array979 */980 public function providerCapitalize()981 {982 return array(983 array(null, null),984 array('', ''),985 array('Cat', 'cat'),986 array('CAt', 'cAt')987 );988 }...

Full Screen

Full Screen

SerializedDataToIniConverter.php

Source:SerializedDataToIniConverter.php Github

copy

Full Screen

...67 self::$index += 2; // <type>:68 switch ($type) {69 case 's':70 $length = intval(71 StringUtils::substringFromTo(self::$value, self::$index, strpos(self::$value, ':', self::$index))72 );73 self::$index += strlen($length) + 2; // :"74 $str = substr(self::$value, self::$index, $length);75 self::$index += strlen($str) + 2; // ";76 return ['type' => 'string', 'value' => $str];77 case 'i':78 $number = StringUtils::substringFromTo(79 self::$value,80 self::$index,81 strpos(self::$value, ';', self::$index)82 );83 self::$index += strlen($number) + 1; // ;84 return ['type' => 'int', 'value' => intval($number)];85 case 'd':86 $number = StringUtils::substringFromTo(87 self::$value,88 self::$index,89 strpos(self::$value, ';', self::$index)90 );91 self::$index += strlen($number) + 1; // ;92 return ['type' => 'double', 'value' => doubleval($number)];93 case 'b':94 $strVal = StringUtils::substringFromTo(95 self::$value,96 self::$index,97 strpos(self::$value, ';', self::$index)98 );99 self::$index += 2; // <0|1>;100 return ['type' => 'boolean', 'value' => $strVal === '1'];101 case 'a':102 $length = intval(103 StringUtils::substringFromTo(self::$value, self::$index, strpos(self::$value, ':', self::$index))104 );105 self::$index += strlen($length) + 2; // :{106 $subItems = [];107 for ($i = 0; $i < $length; $i++) {108 $key = self::parseSerializedString()['value'];109 $value = self::parseSerializedString();110 $subItems[$key] = $value;111 }112 self::$index += 1; // }113 return ['type' => 'array', 'value' => $subItems];114 case 'O':115 $classNameLength = intval(116 StringUtils::substringFromTo(self::$value, self::$index, strpos(self::$value, ':', self::$index))117 );118 self::$index += strlen($classNameLength) + 2; // :"119 $className = substr(self::$value, self::$index, $classNameLength);120 self::$index += $classNameLength + 2; // ":121 $attributeCount = intval(122 StringUtils::substringFromTo(self::$value, self::$index, strpos(self::$value, ':', self::$index))123 );124 self::$index += strlen($attributeCount) + 2; // :{125 $attribute = [];126 for ($i = 0; $i < $attributeCount; $i++) {127 $attributeName = self::parseSerializedString()['value'];128 $attributeName = str_replace("\0*\0", '*', $attributeName);129 $attributeName = str_replace("\0{$className}\0", '-', $attributeName);130 $attributeValue = self::parseSerializedString();131 $attribute[$attributeName] = $attributeValue;132 }133 self::$index += 1; // }134 return ['type' => 'object', 'class' => $className, 'value' => $attribute];135 case 'N':136 return ['type' => 'null'];137 case 'r':138 case 'R':139 $number = StringUtils::substringFromTo(140 self::$value,141 self::$index,142 strpos(self::$value, ';', self::$index)143 );144 self::$index += strlen($number) + 1; // ;145 return ['type' => $type === 'r' ? '*pointer*' : '*reference*', 'value' => intval($number)];146 default:147 return [];148 }149 }150 public static function convertParsingResultToIni($key, $parsingResult)151 {152 $type = $parsingResult['type'];153 switch ($type) {...

Full Screen

Full Screen

ArrayUtils.php

Source:ArrayUtils.php Github

copy

Full Screen

...235 $securityMode = SecurityMode::STRIP_TAGS;236 $defaultValue = null;237 if (StringUtils::startsWith($rule, 'i:')) {238 $type = 2;239 $rule = StringUtils::substringAfter($rule, ':');240 } else if (StringUtils::startsWith($rule, 'd:')) {241 $type = 3;242 $rule = StringUtils::substringAfter($rule, ':');243 } else if (StringUtils::startsWith($rule, 's:')) {244 $rule = StringUtils::substringAfter($rule, ':');245 } else if (StringUtils::startsWith($rule, 'a:')) {246 $type = 4;247 $rule = StringUtils::substringAfter($rule, ':');248 }249 $s1 = '';250 251 switch ($type) {252 case 1:253 if (StringUtils::endsWith($rule, ':0')) {254 $s1 = StringUtils::substringBeforeLast($rule, ':');255 $securityMode = SecurityMode::NONE;256 } else if (StringUtils::endsWith($rule, ':1')) {257 $s1 = StringUtils::substringBeforeLast($rule, ':');258 $securityMode = SecurityMode::HTML_PURIFY;259 } else if (StringUtils::endsWith($rule, ':2')) {260 $s1 = StringUtils::substringBeforeLast($rule, ':');261 } else {262 $s1 = $rule;263 }264 break;265 case 2:266 if (strpos($rule, ':') !== false) {267 $defaultValue = StringUtils::substringAfterLast($rule, ':');268 $defaultValue = StringUtils::isInt($defaultValue) ? (int) $defaultValue : PHP_INT_MIN;269 $s1 = StringUtils::substringBeforeLast($rule, ':');270 } else {271 $s1 = $rule;272 }273 $defaultValue = is_int($defaultValue) ? $defaultValue : PHP_INT_MIN;274 break;275 case 3:276 if (strpos($rule, ':') !== false) {277 $defaultValue = StringUtils::substringAfterLast($rule, ':');278 $defaultValue = StringUtils::isFloat($defaultValue) ? bcadd($defaultValue, 0, 2) : null;279 $s1 = StringUtils::substringBeforeLast($rule, ':');280 } else {281 $s1 = $rule;282 }283 $defaultValue = is_string($defaultValue) ? $defaultValue : '0.00';284 break;285 }286 if (empty($s1)) {287 continue;288 }289 if (strpos($s1, '#') !== false) {290 $mapKey = StringUtils::substringBefore($s1, '#');291 $dstKey = StringUtils::substringAfter($s1, '#');292 } else {293 $mapKey = $s1;294 $dstKey = $s1;295 }296 switch ($type) {297 case 2:298 $value = Cast::toInt($arr[$mapKey], is_int($defaultValue) ? $defaultValue : PHP_INT_MIN);299 break;300 case 3:301 $value = Cast::toString($arr[$mapKey]);302 $value = StringUtils::isFloat($value) ? bcadd($value, 0, 2) : $defaultValue;303 break;304 case 4:305 $value = json_decode(Cast::toString($arr[$mapKey]), true);...

Full Screen

Full Screen

AesCbcHmacEncryption.php

Source:AesCbcHmacEncryption.php Github

copy

Full Screen

...54 }55 if ($cekLen % 2 != 0) {56 throw new JoseJwtException('AES-CBC with HMAC encryption expected key of even number size');57 }58 $hmacKey = StringUtils::substring($cek, 0, $cekLen / 2);59 $aesKey = StringUtils::substring($cek, $cekLen / 2, $cekLen / 2);60 $method = sprintf('AES-%d-CBC', $this->keySize / 2);61 $ivLen = openssl_cipher_iv_length($method);62 $iv = $this->randomGenerator->get($ivLen);63 $cipherText = openssl_encrypt($plainText, $method, $aesKey, true, $iv);64 $authTag = $this->computeAuthTag($aad, $iv, $cipherText, $hmacKey);65 return [$iv, $cipherText, $authTag];66 }67 /**68 * @param string $aad69 * @param string|resource $cek70 * @param string $iv71 * @param string $cipherText72 * @param string $authTag73 *74 * @return string75 */76 public function decrypt($aad, $cek, $iv, $cipherText, $authTag)77 {78 $cekLen = StringUtils::length($cek);79 if ($cekLen * 8 != $this->keySize) {80 throw new JoseJwtException(sprintf('AES-CBC with HMAC algorithm expected key of size %s bits, but was given %s bits', $this->keySize, $cekLen * 8));81 }82 if ($cekLen % 2 != 0) {83 throw new JoseJwtException('AES-CBC with HMAC encryption expected key of even number size');84 }85 $hmacKey = StringUtils::substring($cek, 0, $cekLen / 2);86 $aesKey = StringUtils::substring($cek, $cekLen / 2);87 $expectedAuthTag = $this->computeAuthTag($aad, $iv, $cipherText, $hmacKey);88 if (false === StringUtils::equals($expectedAuthTag, $authTag)) {89 throw new IntegrityException('Authentication tag does not match');90 }91 $method = sprintf('AES-%d-CBC', $this->keySize / 2);92 $plainText = openssl_decrypt($cipherText, $method, $aesKey, true, $iv);93 return $plainText;94 }95 /**96 * @param $aad97 * @param $iv98 * @param $cipherText99 * @param $hmacKey100 *101 * @return string102 */103 private function computeAuthTag($aad, $iv, $cipherText, $hmacKey)104 {105 $aadLen = StringUtils::length($aad);106 $max32bit = 2147483647;107 $hmacInput = implode('', [108 $aad,109 $iv,110 $cipherText,111 pack('N2', ($aadLen / $max32bit) * 8, ($aadLen % $max32bit) * 8),112 ]);113 $authTag = $this->hashAlgorithm->sign($hmacInput, $hmacKey);114 $authTagLen = StringUtils::length($authTag);115 $authTag = StringUtils::substring($authTag, 0, $authTagLen / 2);116 return $authTag;117 }118}...

Full Screen

Full Screen

substring

Using AI Code Generation

copy

Full Screen

1require_once("StringUtils.php");2$str = new StringUtils();3echo $str->substring("hello", 1, 3);4require_once("StringUtils.php");5$str = new StringUtils();6echo $str->substring("hello", 1, 3);7require_once("StringUtils.php");8$str = new StringUtils();9echo $str->substring("hello", 1, 3);10require_once("StringUtils.php");11$str = new StringUtils();12echo $str->substring("hello", 1, 3);13require_once("StringUtils.php");14$str = new StringUtils();15echo $str->substring("hello", 1, 3);16require_once("StringUtils.php");17$str = new StringUtils();18echo $str->substring("hello", 1, 3);19require_once("StringUtils.php");20$str = new StringUtils();21echo $str->substring("hello", 1, 3);22require_once("StringUtils.php");23$str = new StringUtils();24echo $str->substring("hello", 1, 3);25require_once("StringUtils.php");26$str = new StringUtils();27echo $str->substring("hello", 1, 3);28require_once("StringUtils.php");29$str = new StringUtils();30echo $str->substring("hello", 1, 3);

Full Screen

Full Screen

substring

Using AI Code Generation

copy

Full Screen

1require_once 'StringUtils.php';2$str = "Hello World";3echo StringUtils::substring($str, 6);4echo "<br>";5echo StringUtils::substring($str, 6, 4);6echo "<br>";7echo StringUtils::substring($str, 6, 8);8echo "<br>";9echo StringUtils::substring($str, 6, 0);10echo "<br>";11echo StringUtils::substring($str, 0, 5);12echo "<br>";13echo StringUtils::substring($str, 0, 11);14echo "<br>";15echo StringUtils::substring($str, 0, 12);16echo "<br>";17echo StringUtils::substring($str, 0, 0);18echo "<br>";19echo StringUtils::substring($str, 0, -1);20echo "<br>";21echo StringUtils::substring($str, -1, 0);22echo "<br>";23echo StringUtils::substring($str, -1, 1);24echo "<br>";25echo StringUtils::substring($str, -1, 5);26echo "<br>";27echo StringUtils::substring($str, -1, -1);28echo "<br>";29echo StringUtils::substring($str, -1, -5);30echo "<br>";31echo StringUtils::substring($str, -5, -1);32echo "<br>";33echo StringUtils::substring($str, -5, 0);34echo "<br>";35echo StringUtils::substring($str, -5, 1);36echo "<br>";37echo StringUtils::substring($str, -5, 5);38echo "<br>";39echo StringUtils::substring($str, -5, 6);40echo "<br>";41echo StringUtils::substring($str, -5, 11);42echo "<br>";43echo StringUtils::substring($str, -5, 12);44echo "<br>";45echo StringUtils::substring($str, -5, 13);46echo "<br>";47echo StringUtils::substring($str, -11, -5);48echo "<br>";49echo StringUtils::substring($str, -11, -1);50echo "<br>";51echo StringUtils::substring($str, -11, 0);52echo "<br>";53echo StringUtils::substring($str, -11, 1);54echo "<br>";55echo StringUtils::substring($str, -11, 5);56echo "<br>";57echo StringUtils::substring($str, -11, 6);58echo "<br>";59echo StringUtils::substring($str, -11, 11);

Full Screen

Full Screen

substring

Using AI Code Generation

copy

Full Screen

1import org.apache.commons.lang3.StringUtils;2{3 public static void main(String[] args)4 {5 String str = "Hello World";6 System.out.println("The string is: " + str);7 System.out.println("The substring is: " + StringUtils.substring(str, 3, 7));8 }9}10public static String capitalize(String str)11public static String capitalize(String str, char... delimiters)12public static String capitalizeFully(String str)13public static String capitalizeFully(String str, char... delimiters)14public static String uncapitalize(String str)15public static String uncapitalize(String str, char... delimiters)16public static String wrap(String str, int wrapLength)17public static String wrap(String str, int wrapLength, String newLineStr, boolean wrapLongWords)18import org.apache.commons.lang3.text.WordUtils;19{20 public static void main(String[] args)21 {22 String str = "welcome to javaTpoint";23 System.out.println("The string is: " + str);24 System.out.println("The capitalized string is: " + WordUtils.capitalize(str));25 System.out.println("The capitalized string is: " + WordUtils.capitalizeFully(str));26 System.out.println("The uncapitalized string is: " + WordUtils.uncapitalize(str));27 System.out.println("The wrapped string is: " + WordUtils.wrap(str, 15));28 }29}

Full Screen

Full Screen

substring

Using AI Code Generation

copy

Full Screen

1import com.wipro.util.StringUtils;2public class Ex1 {3 public static void main(String[] args) {4 System.out.println(StringUtils.substring("Hello", 2));5 }6}

Full Screen

Full Screen

substring

Using AI Code Generation

copy

Full Screen

1include "StringUtils.php";2$string = "Hello World";3echo "Original string is ".$string."<br>";4$substring = StringUtils::substring($string, 1, 5);5echo "Substring is ".$substring."<br>";6$reverse = StringUtils::reverse($string);7echo "Reversed string is ".$reverse."<br>";8$replace = StringUtils::replace($string, "Hello", "Hi");9echo "Replaced string is ".$replace."<br>";10$compare = StringUtils::compare($string, "Hello World");11echo "Comparison of strings is ".$compare."<br>";12$compareIgnoreCase = StringUtils::compareIgnoreCase($string, "HELLO WORLD");13echo "Comparison of strings is ".$compareIgnoreCase."<br>";14$startsWith = StringUtils::startsWith($string, "Hello");15echo "String starts with Hello ".$startsWith."<br>";16$endsWith = StringUtils::endsWith($string, "World");17echo "String ends with World ".$endsWith."<br>";18$contains = StringUtils::contains($string, "Hello");19echo "String contains Hello ".$contains."<br>";20$isEmpty = StringUtils::isEmpty($string);21echo "String is empty ".$isEmpty."<br>";22$trim = StringUtils::trim($string);23echo "String after trimming is ".$trim."<br>";24$toLowerCase = StringUtils::toLowerCase($string);25echo "String in lower case is ".$toLowerCase."<br>";26$toUpperCase = StringUtils::toUpperCase($string);27echo "String in upper case is ".$toUpperCase."<br>";28$length = StringUtils::length($string);29echo "Length of string is ".$length."<br>";30$indexOf = StringUtils::indexOf($string, "o");31echo "Index of o in string is ".$indexOf."<br>";32$lastIndexOf = StringUtils::lastIndexOf($string, "l");

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Gherkin-php automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Trigger substring code on LambdaTest Cloud Grid

Execute automation tests with substring on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful