Skip to content

Instantly share code, notes, and snippets.

@blood72
Last active April 23, 2020 07:45
Show Gist options
  • Save blood72/bc27beaa82405bb224edeb1b2d955400 to your computer and use it in GitHub Desktop.
Save blood72/bc27beaa82405bb224edeb1b2d955400 to your computer and use it in GitHub Desktop.
Return character(s) for each position(s) to string
<?php
use Illumniate\Support\Str;
if (! Str::hasMacro('character')) {
Str::macro('character', static function ($value, $positions, $glue = '') {
if (! is_array($positions)) {
$positions = [$positions];
}
$result = '';
foreach ($positions as $key => $position) {
if ($key !== array_key_first($positions)) {
$result .= $glue;
}
$result .= Str::substr($value, $position, 1);
}
return $result;
});
}
if (! Str::hasMacro('char')) {
Str::macro('char', static function ($value, $positions, $glue = '') {
return Str::character($value, $positions, $glue);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment