DEV Community

Discussion on: 5 tips in PHP 🐘

Collapse
bdelespierre profile image
Benjamin Delespierre

Here's another one for you: extract variables from string using patterns

$str = "123:456";

preg_match('/(\d+):(\d+)/', $str, $matches);

list(, $a, $b) = $matches + [null, null, null];

var_dump($a, $b); // 123, 456
Enter fullscreen mode Exit fullscreen mode