Need Help Forcing REGEX to Ignore = and Break on Space

I asked this question over on StackOverflow and it’s getting ignored there, so hopefully some brilliant minds on Sitepoint can help me. I’m horrid with complex REGEXs and have spent a lot of time trying to make this work, but it’s just not happening.

Essentially, at the time this REGEX is ran, the attributes inside an HTML or custom tag delimited by has already ran via a preg_replace_callback. So all this REGEX sees is two possible strings:

attribute1=some value here attribute2=alert('this is another value with javascript'); attribute3...attribute4...etc...

or

attribute1="some value here" attribute2="alert('this is another value with javascript');" attribute3...attribute4...etc...

The regex doesn’t need to break on quotes as I will either add or let them be when they are replacing tokens in the replace string. I just need the regex to break on the space before an attribute’s name.

The current REGEX I have is this:

#(\\s+[^\\s=]+)\\s*=\\s*([^\\s=]+(?>\\s+[^\\s=]+)*(?!\\s*=))#i

It works exactly like I want, but it breaks prematurely if there’s an = in the attribute’s value that I want. So for instance, if you were doing an onClick attribute in a tag and the javascript required you to list a variable and it’s value. It’d break on the last letter in the variable’s name before the =.

String to check against:

[div onClick=window.open('http%3A%2F%2Fwww.sitepoint.com%2Ffakeindex.php%3Fsomevariable%3Dsomevalue','popup','scrollbars=yes,resizable=yes,width=716,height=540,left=0,top=0,ScreenX=0,ScreenY=0');]Div Content[/div]

The above will break on the letter prior to the =, so in this case that the URL is encoded, it breaks on “s” in “scrollbars=yes”. It needs to be able to go all the way through to the semicolon.

So, if someone could help me to modify this REGEX slightly so that it only breaks if it finds a space then attribute name and = that’d be awesome.

preg_split(‘#\s(\w*?)=#’,$string,-1,PREG_SPLIT_DELIM_CAPTURE);
You’ll have to strip the tags off of the beginning and end of the string, though.