flash gordon Posted January 23, 2007 Share Posted January 23, 2007 Hi[code]$contents = '<Template id="0" url="Templates/foo" name="Fudcake" >';$contents .= '<fields>Hello</fields>';$contents .= '</Template>';$contents .= '<Template id="0" url="Templates/foo" name="Flash" >';$contents .= '<fields>Hello</fields>';$contents .= '</Template>';$myVar = "Flash";$pattern = '/<Template .+? name="' . $myVar . '"/s';preg_match_all($pattern, $contents, $matches);print_r($matches);exit();// output: // <Template id="0" url="Templates/foo" name="Fudcake" ><fields>Hello</fields></Template><Template id="0" url="Templates/foo" name="Flash"[/code]My goal and what I think it should output is simply [b][i]<Template id="0" url="Templates/foo" name="Flash"[/i][/b].Can anyone see my error?:) Quote Link to comment Share on other sites More sharing options...
Eclesiastes Posted January 23, 2007 Share Posted January 23, 2007 /<Template[^>]+name="' . $myVar .'"/ Quote Link to comment Share on other sites More sharing options...
flash gordon Posted January 23, 2007 Author Share Posted January 23, 2007 Cheers mate :)I don't understand [^>] if anyone what to help explain. From my understanding it means [] == this set ^ == not > == > so I'm not sure why that works.But thanks again.:) Quote Link to comment Share on other sites More sharing options...
effigy Posted January 23, 2007 Share Posted January 23, 2007 Initially you were not confining your regex to stay within the tag, but only to stop when it first encountered a name attribute. [tt][^>]+ [/tt]tells the pattern to match one or more characters that are not ">", thus staying within the tag. Once the engine hits the end of the tag, it backtracks attempting to satisfy the rest of the pattern ("n", then "a", then "m", then "e", and so on). If the name attribute happened to be towards the beginning of the tag--unlike your example--[tt][^>]+?[/tt] would be a better fit. Quote Link to comment Share on other sites More sharing options...
flash gordon Posted January 24, 2007 Author Share Posted January 24, 2007 That makes sense.....Reg Ex's are an art form!Thanks mate,:) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.