Skip to content

Instantly share code, notes, and snippets.

@WinterSilence
Created December 9, 2021 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WinterSilence/50fd5511b58f1bb86265dcdba2881d65 to your computer and use it in GitHub Desktop.
Save WinterSilence/50fd5511b58f1bb86265dcdba2881d65 to your computer and use it in GitHub Desktop.
SCSS/SASS port of PHP function substr()
/// Returns part of string.
/// Similar to PHP `substr()`: `substr("abc", 2, 1)` - "c", `substr("abc", 1)` - "bc", `substr("abcd", -3, -1)` - "bc".
///
/// @param {String} $str The input string
/// @param {Number} $offset The returned string will start at the offset'th position in string, counting from zero
/// @param {Number} $length The string returned will contain at most length characters beginning from offset (optional)
/// @return {String}
/// @see https://www.php.net/manual/en/function.substr
@function substr($str, $offset, $length: 0) {
$length: if($length < 0, $length - 1, if($length == 0, str-length($str), $length));
@return str-slice(str-slice($str, $offset), 1, $length);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment