Regex multiple match substring

You can achieve what you need by using two steps.

You could use this regex to validate the format:

\^\(\w+(?:\|\w+)*\)\$

Working demo

Once you validated the right strings you can use a function like this:

$str = "^(100|500|1000|2000|3000)$";
$arr = preg_split ("/\W+/" , $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($arr);

Output:

Array
(
    [0] => 100
    [1] => 500
    [2] => 1000
    [3] => 2000
    [4] => 3000
)

Leave a Comment