I need to get a value from an array using the dot notation. For example:
$arr = [ 'first' => [ 'second' => [ 'third' => 'hello' ] ] ]; $value = array_dot($arr, 'first.second.third'); // returns "hello"
So what I need to do is check if it’s a valid string by which we could get a value from array.
Constraints:
- non-whitespace characters only
- contains at least one dot
- cannot start or end with a dot
- every dot must have a non-dot substring before and after it
Cases:
`value.inside.array` valid `val-2.of.array` valid `.var2.in.arr` invalid `dot.notation.` invalid `value. dot` invalid `consecutive....dots` invalid
Advertisement
Answer
Try this:
^[^.s]S*.S*[^.s]$
Explanation:
^[^.s] Start with any non-dot and non-white character S* Any non space characters . Force at least one dot S* Any non space characters [^.s]$ End with any non-dot and non-white character
Demo here.