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?:) Link to comment https://forums.phpfreaks.com/topic/35321-stop-my-simple-reg-exp-mistake/ Share on other sites More sharing options...
Eclesiastes Posted January 23, 2007 Share Posted January 23, 2007 /<Template[^>]+name="' . $myVar .'"/ Link to comment https://forums.phpfreaks.com/topic/35321-stop-my-simple-reg-exp-mistake/#findComment-167113 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.:) Link to comment https://forums.phpfreaks.com/topic/35321-stop-my-simple-reg-exp-mistake/#findComment-167572 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. Link to comment https://forums.phpfreaks.com/topic/35321-stop-my-simple-reg-exp-mistake/#findComment-167580 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,:) Link to comment https://forums.phpfreaks.com/topic/35321-stop-my-simple-reg-exp-mistake/#findComment-167758 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.