<?php
$input = [
'new message from Bob [22:105:3905:534]',
'user Dylan posted a question in section General',
'new message from Mary(gold) [19504:8728:18524:78941]'
];
$formats = [
'/new message from (\w+) \[(\d+):(\d+):(\d+):(\d+)\]/', // this would actually be something like '/(?<=new message from )(.*)(?=[)(.*)(?=:)(.*)(?=:)(.*)(?=:)(.*)(?=])/'
'/user (\w+) posted a question in section ([\w ]+)/',
'/new message from (\w+)\((\w+)\) \[(\d+):(\d+):(\d+):(\d+)\]/',
];
foreach ($input as $line) {
foreach ($formats as $key => $format) {
$data = [];
if (preg_match($format, $line, $data)) {
array_shift($data);
echo 'format: ' . $key . ', data: ' . var_export($data, true) . "\n";
continue;
}
}
}
- Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
- format: 0, data: array (
0 => 'Bob',
1 => '22',
2 => '105',
3 => '3905',
4 => '534',
)
format: 1, data: array (
0 => 'Dylan',
1 => 'General',
)
format: 2, data: array (
0 => 'Mary',
1 => 'gold',
2 => '19504',
3 => '8728',
4 => '18524',
5 => '78941',
)
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
format: 0, data: array (
0 => 'Bob',
1 => '22',
2 => '105',
3 => '3905',
4 => '534',
)
format: 1, data: array (
0 => 'Dylan',
1 => 'General',
)
format: 2, data: array (
0 => 'Mary',
1 => 'gold',
2 => '19504',
3 => '8728',
4 => '18524',
5 => '78941',
)
preferences:
253.6 ms | 402 KiB | 287 Q