- var_dump: documentation ( source)
- preg_match_all: documentation ( source)
<?php
$re = '%^
((?=x))?
((?=y))?
(\w)
$%mx';
$str = 'x
y
z';
preg_match_all($re, $str, $matches, PREG_SET_ORDER | PREG_UNMATCHED_AS_NULL, 0);
// Print the entire match result
var_dump($matches);