public function testStrReplaceFirst() { $key = 'foo.*.bar.*'; $results = Str::strReplaceFirst('*', '0', $key); $this->assertEquals('foo.0.bar.*', $results); $results = Str::strReplaceFirst('no', '0', $results); $this->assertEquals('foo.0.bar.*', $results); }
/** * Accepts a dot-notation array key with wildcards and expands them * into an array of dot-notation array keys in numerical order. * * @param string $key - dot notation * @param int $size - number of keys to generate per wildcard. Defaults to 10. * @return array */ public static function expandKeys($key, $size = 10) { $keys = []; $original_key = $key; for ($i = 0; $i < $size; $i++) { $k = Str::strReplaceFirst('*', $i, $original_key); if (strpos($k, '*') !== false) { $sub_keys = static::expandKeys($k, $size); foreach ($sub_keys as $sk) { $keys[] = $sk; } } else { $keys[] = $k; } } return $keys; }