Skip to content

Instantly share code, notes, and snippets.

@cgray
Created July 29, 2015 18:33
Show Gist options
  • Save cgray/c348928f619b3eb68103 to your computer and use it in GitHub Desktop.
Save cgray/c348928f619b3eb68103 to your computer and use it in GitHub Desktop.
<?php
$subject = <<<DATA
property=value property2="value2" property3='value3'
DATA;
$pattern = '(
(?:^|\s)
(?<name>[a-z]\w+)
(?:=)
(?:(?J)
(?:\'(?<value>[^\']+)\')|
(?:"(?<value>[^"]*)")|
(?<value>[^\s,]+)
)
)xiu';
preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);
$result = [];
foreach ($matches as $match) {
$result[$match['name']] = $match['value'];
}
var_dump($result);
@cgray
Copy link
Author

cgray commented Jul 29, 2015

preg to get an associative array from a htmlesqe attribute string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment