Last active
April 23, 2020 07:45
-
-
Save blood72/bc27beaa82405bb224edeb1b2d955400 to your computer and use it in GitHub Desktop.
Return character(s) for each position(s) to string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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