- substr: documentation ( source)
- preg_replace_callback: documentation ( source)
- print_r: documentation ( source)
- array_merge: documentation ( source)
- preg_split: documentation ( source)
<?php
$input = <<< TXT
Blah
Diddel
Foo
Bar1
Bar2
One
XXY
Password%1 = something
Password%2 = other
Password%3 = nuffing
Completely different stuff here
RandomS6}=2
User%1 = Henry
User%2 = Marcus
User%3 = SecretAdmin
More stuff
Even more stuff
Loads of stuff
TXT;
$matches = [];
$string = preg_replace_callback(
'~^(?>User.+|Password.+|XXY)$~m',
function ($match) use (&$matches) {
@$matches[substr($match[0], 0, 4)] .= $match[0].PHP_EOL;
return null;
},
$input
);
print_r(array_merge($matches, preg_split('/^\s+$/m', $string)));