I am having a bit of a problem with some regex I did for a project of mine (please keep in mind that I am a beginner at regex which shows in the follwoing example). I am having a bit of a problem with a piece of xml code from which I am trying to extract certain parts of it using an associated pattern.
<banner piclink="pic" urlactive="url_active" urltarget="globaltgt" urllink="globallink" timevar="globaldelay" swf="0" smooth="1" name="name" alt="alternate" />
I am using the following regular expression to obtain the piclink, urlactive, urltarget, urllink and timevar using preg_match_all:
/piclink=\"(?<pic>.+)\".+urltarget=\"(?<target>.+)\".+urllink=\"(?<url>.*)\".+timevar=\"(?<delay>.*)\"/iU
So far so good everything works right however, I am now trying to capture with association the name and alt tags which are optional as in they don’t always appear. I have tried to put them in parenthesis followed by a ? to indicate that they are optional like such:
(name=\"(?<name>.*)\")?
However the $matches[‘name’] array is always empty, I do not know where I am messing up but I have tried all sorts of combinations and all of them result in an empty result except for when I put (?: at the end and encapsulate everything from swf= onwards then it does return like 115 results in the array which is not acceptabe as the result is like $matches[‘name’][X] = result, where x is sometimes 1 other times its at 109 for some reason.
I agree that something like
SimpleXMLwould be better but if you want to get dirty, you can use lookaheads to try to match with the remaining characters.