<?php
$text = 'some text [person name="Jackson" family="Smith"] text [vehicle brand="Benz" type="SUV" doors="4" seats="7"] and [invalid closed="false" monkeywrench [lonetag] text [single gender="female"]';
foreach (preg_match_all('~\[(\w+)(?=(?: \w+="\w+")*])(]?)|(?:\G(?!^) (\w+)="(\w+)")~', $text, $out, PREG_SET_ORDER) ? $out : [] as $matches) {
if ($matches[2]) {
$result[$matches[1]] = [];
} elseif (!isset($matches[3])) {
$tag = $matches[1];
} else {
$result[$tag][$matches[3]] = $matches[4];
}
}
var_export($result);
- Output for 7.1.25 - 7.1.32, 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.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- array (
'person' =>
array (
'name' => 'Jackson',
'family' => 'Smith',
),
'vehicle' =>
array (
'brand' => 'Benz',
'type' => 'SUV',
'doors' => '4',
'seats' => '7',
),
'lonetag' =>
array (
),
'single' =>
array (
'gender' => 'female',
),
)
preferences:
156.52 ms | 408 KiB | 5 Q