| <?php |
| |
| namespace TypedPHP\Functions\StringFunctions; |
| |
| use TypedPHP\Functions\TypeFunctions; |
| |
| |
| |
| |
| |
| |
| function isExpression($variable) |
| { |
| return TypeFunctions\isExpression($variable); |
| } |
| |
| |
| |
| |
| |
| |
| function isArray($variable) |
| { |
| return TypeFunctions\isArray($variable); |
| } |
| |
| |
| |
| |
| |
| |
| |
| function startsWith($haystack, $needle) |
| { |
| if (isExpression($needle)) { |
| return startsWithExpression($haystack, $needle); |
| } |
| |
| return startsWithString($haystack, $needle); |
| } |
| |
| |
| |
| |
| |
| |
| |
| function startsWithString($haystack, $needle) |
| { |
| return slice($haystack, 0, length($needle)) === $needle; |
| } |
| |
| |
| |
| |
| |
| |
| |
| function startsWithExpression($haystack, $needle) |
| { |
| $pattern = slice($needle, 1, length($needle) - 2); |
| |
| return matches($haystack, "#^{$pattern}#"); |
| } |
| |
| |
| |
| |
| |
| |
| |
| function endsWith($haystack, $needle) |
| { |
| if (isExpression($needle)) { |
| return endsWithExpression($haystack, $needle); |
| } |
| |
| return endsWithString($haystack, $needle); |
| } |
| |
| |
| |
| |
| |
| |
| |
| function endsWithString($haystack, $needle) |
| { |
| return slice($haystack, -1 * length($needle)) === $needle; |
| } |
| |
| |
| |
| |
| |
| |
| |
| function endsWithExpression($haystack, $needle) |
| { |
| $pattern = slice($needle, 1, length($needle) - 2); |
| |
| return matches($haystack, "#{$pattern}$#"); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| function indexOf($haystack, $needle, $offset = 0) |
| { |
| if (isExpression($needle)) { |
| return indexOfExpression($haystack, $needle, $offset); |
| } |
| |
| return indexOfString($haystack, $needle, $offset); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| function indexOfString($haystack, $needle, $offset = 0) |
| { |
| $index = -1; |
| $match = strpos($haystack, $needle, $offset); |
| |
| if ($match !== false) { |
| $index = $match; |
| } |
| |
| return $index; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| function indexOfExpression($haystack, $needle, $offset = 0) |
| { |
| $index = -1; |
| |
| $match = preg_match( |
| $needle, $haystack, $matches, |
| PREG_OFFSET_CAPTURE, $offset |
| ); |
| |
| if ($match) { |
| $index = $matches[0][1]; |
| } |
| |
| return $index; |
| } |
| |
| |
| |
| |
| |
| |
| function length($string) |
| { |
| return strlen($string); |
| } |
| |
| |
| |
| |
| |
| |
| |
| function matches($haystack, $needle) |
| { |
| if (isExpression($needle)) { |
| return matchesExpression($haystack, $needle); |
| } |
| |
| return matchesString($haystack, $needle); |
| } |
| |
| |
| |
| |
| |
| |
| |
| function matchesString($haystack, $needle) |
| { |
| if ($needle === $haystack) { |
| return true; |
| } |
| |
| return false; |
| } |
| |
| |
| |
| |
| |
| |
| |
| function matchesExpression($haystack, $needle) |
| { |
| if (preg_match($needle, $haystack)) { |
| return true; |
| } |
| |
| return false; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| function replace($haystack, $needle, $replacement) |
| { |
| if (isArray($needle) and isArray($replacement)) { |
| return replaceWithArray( |
| $haystack, $needle, $replacement |
| ); |
| } |
| |
| if (isExpression($needle)) { |
| return replaceWithExpression( |
| $haystack, $needle, $replacement |
| ); |
| } |
| |
| return replaceWithString( |
| $haystack, $needle, $replacement |
| ); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| function replaceWithArray($haystack, array $needle, array $replacement) |
| { |
| foreach ($needle as $i => $next) { |
| $haystack = replace($haystack, $next, $replacement[$i]); |
| } |
| |
| return $haystack; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| function replaceWithString($haystack, $needle, $replacement) |
| { |
| return str_replace($needle, $replacement, $haystack); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| function replaceWithExpression($haystack, $needle, $replacement) |
| { |
| return (string) preg_replace( |
| $needle, $replacement, $haystack |
| ); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| function slice($string, $offset = 0, $limit = 0) |
| { |
| if ($limit == 0) { |
| return substr($string, $offset); |
| } |
| |
| return substr($string, $offset, $limit); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| function split($haystack, $needle = null, $limit = 0) |
| { |
| if ($needle === null) { |
| return splitWithNull($haystack, $limit); |
| } |
| |
| if (isExpression($needle)) { |
| return splitWithExpression($haystack, $needle, $limit); |
| } |
| |
| return splitWithString($haystack, $needle, $limit); |
| } |
| |
| |
| |
| |
| |
| |
| |
| function splitWithNull($haystack, $limit = 0) |
| { |
| if ($limit === 0) { |
| return str_split($haystack); |
| } |
| |
| return str_split($haystack, $limit); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| function splitWithString($haystack, $needle, $limit = 0) |
| { |
| if ($limit === 0) { |
| return explode($needle, $haystack); |
| } |
| |
| return explode($needle, $haystack, $limit); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| function splitWithExpression($haystack, $needle, $limit = 0) |
| { |
| if ($limit === 0) { |
| return preg_split($needle, $haystack); |
| } |
| |
| return preg_split($needle, $haystack, $limit); |
| } |
| |
| |
| |
| |
| |
| |
| |
| function trim($haystack, $needle) |
| { |
| if (isExpression($needle)) { |
| return trimWithExpression($haystack, $needle); |
| } |
| |
| return trimWithString($haystack, $needle); |
| } |
| |
| |
| |
| |
| |
| |
| |
| function trimWithString($haystack, $needle) |
| { |
| return \trim($haystack, $needle); |
| } |
| |
| |
| |
| |
| |
| |
| |
| function trimWithExpression($haystack, $needle) |
| { |
| $pattern = slice($needle, 1, length($needle) - 2); |
| |
| return (string) preg_replace( |
| "#^{$pattern}|{$pattern}$#", "", $haystack |
| ); |
| } |
| |
| |
| |
| |
| |
| |
| |
| function trimLeft($haystack, $needle) |
| { |
| if (isExpression($needle)) { |
| return trimLeftWithExpression($haystack, $needle); |
| } |
| |
| return trimLeftWithString($haystack, $needle); |
| } |
| |
| |
| |
| |
| |
| |
| |
| function trimLeftWithString($haystack, $needle) |
| { |
| return ltrim($haystack, $needle); |
| } |
| |
| |
| |
| |
| |
| |
| |
| function trimLeftWithExpression($haystack, $needle) |
| { |
| $pattern = slice($needle, 1, length($needle) - 2); |
| |
| return (string) preg_replace( |
| "#^{$pattern}#", "", $haystack |
| ); |
| } |
| |
| |
| |
| |
| |
| |
| |
| function trimRight($haystack, $needle) |
| { |
| if (isExpression($needle)) { |
| return trimRightWithExpression($haystack, $needle); |
| } |
| |
| return trimRightWithString($haystack, $needle); |
| } |
| |
| |
| |
| |
| |
| |
| |
| function trimRightWithString($haystack, $needle) |
| { |
| return rtrim($haystack, $needle); |
| } |
| |
| |
| |
| |
| |
| |
| |
| function trimRightWithExpression($haystack, $needle) |
| { |
| $pattern = slice($needle, 1, length($needle) - 2); |
| |
| return (string) preg_replace( |
| "#{$pattern}$#", "", $haystack |
| ); |
| } |