I am having a problem with some regex that I made for my project (remember that I am new to regex, which is shown in the following example). I have a bit of a problem with a piece of xml code from which I am trying to extract some parts of it using a linked template.
<banner piclink="pic" urlactive="url_active" urltarget="globaltgt" urllink="globallink" timevar="globaldelay" swf="0" smooth="1" name="name" alt="alternate" />
I use the following regular expression to get piclink, urlactive, urltarget, urllink and timevar using preg_match_all:
/piclink=\"(?<pic>.+)\".+urltarget=\"(?<target>.+)\".+urllink=\"(?<url>.*)\".+timevar=\"(?<delay>.*)\"/iU
So far, everything is working well, however now I am trying to grab the name and alt tags with the association, which are optional since they do not always appear. I tried putting them in brackets, and then? to indicate that they are optional, for example:
(name=\"(?<name>.*)\")?
However, the $ matches ['name'] array is always empty, I donβt know where I got confused, but I tried all kinds of combinations, and all of them produce empty results, except when I put (?: In the end and encapsulate everything from swf = forward, then it returns as 115 results in the array, which does not accept, since the result is similar to $ matches ['name'] [X] = result, where x sometimes happens 1 time for 109 for some reason.
source
share