regex to extract names and values of attributes

Optimal

New Member
I have the following possible strings that I need to turn into arrays so I can feed them into an html generator. I am not staring with html or XML, I am trying to create a shorthand that will allow me to populate my html objects much simpler and faster with more readable code.id='moo'id = "foo" type= doo value ='http://stackoverflow.com/questions/3687511/do/"o'on_click='monkeys("bobo")'I need to pull out the attribs and their corresponding values. These attrib strings are not associated with an html or xml tag. And I would like to do it with 1 to 3 regular expressions
  • The value may be encapsulated by either single or double quotes
  • If the value is encapsulated by quotes it may also contain whitespace, quotes different from encapsulating quotes or escaped quotes that are same as the encapsulating quotes.
  • There may or may not be whitespaces between the attrib and =, and the = and value.
The eventual results should look like:array(1) { [id] => moo}array(3) { [id] => foo [type] => doo [value] => do"o}array(1) { [on_click] => monkeys("bobo")}but if it turns out like:array(2) { [0] => id [1] => moo}array(6) { [0] => id [1] => moo [2] => class [3] => foo [4] => value [5] => do"o}array(2) { [0] => on_click [1] => monkeys("bobo")}I can re-arrange it from there.Some previous regexes I have tried to use and their issues:
  • \[code\]/[\s]+/\[/code\] - Return attrib/value pairs only if there was no whitespace around the =
  • \[code\]/(?<==)(\".*\"|'.*'|.*)$/\[/code\] - Returns value including the encapsulating quotes. It does ignore escaped quotes within the value though
  • \[code\]/^[^=]*/\[/code\] - Returns the attribute just fine. regardless of whitespace between attrib and =
 
Back
Top