superaktieboy Posted February 22, 2009 Share Posted February 22, 2009 hi im trying to make a template parser in php.. am a bit stuck tho.. im trying to parse the following: {blockname.blockname.varname} that seems to work fine if there is only one of those. but if i have mutliple vars next to eachother like this: {blockname.blockname.varname1} {blockname.blockname.varname2} the same thing doesn't work.. my regex currently is: /(\{([^\}](.*?\.)+(.*?)+)\})/ and im using preg_match to find them. however, it doesn't seem to work, i keep getting the following as the result: Array ( [0] => {blockname.blockname.varname1} {blockname.blockname.varname2} [1] => {blockname.blockname.varname1} {blockname.blockname.varname2} [2] => blockname.blockname.varname1} {blockname.blockname.varname2 [3] => blockname. [4] => ) no i use the third array argument ([2]).. which is supposed to be blockname.blockname.varname1 (without the brackets).. but if i have multiple vars on the same line, i get all the stuff inbetween the first and the last var. as u can see i have also tried adding [^\}] but it doesn't seem to work either.. how can i get multiple of these vars (without the brackets if possible) if they're on the same line? coz what i put down for the regex seems to fail epicly. thanks for your help super... Quote Link to comment Share on other sites More sharing options...
.josh Posted February 22, 2009 Share Posted February 22, 2009 are those the only things on the line? Quote Link to comment Share on other sites More sharing options...
.josh Posted February 22, 2009 Share Posted February 22, 2009 Can you give actual examples of the context these things are in? I'm thinking it would be easier to just ~\{([^\}]+)\}~ and then explode at the dot. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted February 22, 2009 Share Posted February 22, 2009 If I understand correctly, would this be along the lines of what you are looking for? $str = 'Some garbge...{blockname.blockname.varname1}...more garbage...{blockname.blockname.varname2}.. and more garbge yet...{blockname.blockname.varname3}'; preg_match_all('#{(?:[a-z0-9]+\.?)+}#i', $str, $matches); echo "<pre>".print_r($matches[0], true); output: Array ( [0] => {blockname.blockname.varname1} [1] => {blockname.blockname.varname2} [2] => {blockname.blockname.varname3} ) this of course makes assumptions like the characters between { and the dots and } are a-z (case insensitive) or 0-9. It's a quick and dirty method which can be made more precise, but does the job for the examples you provided. Is this what you're looking for? Quote Link to comment Share on other sites More sharing options...
.josh Posted February 22, 2009 Share Posted February 22, 2009 nrg_alpha I'm going to go out on a limb and assume that since this is for a template, that whatever is inbetween {...} is not going to be a { or } (in other words, those are the delimiters) so would be more efficient to just use my regex. Of course, you know what they say about assuming... Quote Link to comment Share on other sites More sharing options...
superaktieboy Posted February 22, 2009 Author Share Posted February 22, 2009 are those the only things on the line? nope not necessary Can you give actual examples of the context these things are in? I'm thinking it would be easier to just ~\{([^\}]+)\}~ and then explode at the dot. hmm sounds like an idea.. im gonna see if nrg_alpha's method works.. if it doesn't ill be using this.. thanks If I understand correctly, would this be along the lines of what you are looking for? $str = 'Some garbge...{blockname.blockname.varname1}...more garbage...{blockname.blockname.varname2}.. and more garbge yet...{blockname.blockname.varname3}'; preg_match_all('#{(?:[a-z0-9]+\.?)+}#i', $str, $matches); echo "<pre>".print_r($matches[0], true); output: Array ( [0] => {blockname.blockname.varname1} [1] => {blockname.blockname.varname2} [2] => {blockname.blockname.varname3} ) this of course makes assumptions like the characters between { and the dots and } are a-z (case insensitive) or 0-9. It's a quick and dirty method which can be made more precise, but does the job for the examples you provided. Is this what you're looking for? yeah i think that is it thanks Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted February 22, 2009 Share Posted February 22, 2009 True enough. If the goal is just to get what's between { and }, your solution is fine (had I understood it that way, I would have proposed almost the same thing - see message below). I was under the assumption of a specific format..(I am seeing the examples provided, and I start thinking { something . something . somethingx } and forgot about the no brackets thing.. In your sample, you don't need to escape the { and } characters.. you could simply do this: ~{([^}]+)}~ as these curly braces are already understood / treated as literals. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted February 22, 2009 Share Posted February 22, 2009 superaktieboy, CV's method works if all you want is what is between { and }. If there are specifics that need to be met within those braces, then you should specify (unless you example is about as specific as needed.. even then, my solution would still allow for something like: {blockname1.blockname1.varname1} I was admittedly making it somewhat open ended (not as much as CV's granted). So if there is a specific format, let us know. Quote Link to comment Share on other sites More sharing options...
superaktieboy Posted February 23, 2009 Author Share Posted February 23, 2009 superaktieboy, CV's method works if all you want is what is between { and }. If there are specifics that need to be met within those braces, then you should specify (unless you example is about as specific as needed.. even then, my solution would still allow for something like: {blockname1.blockname1.varname1} I was admittedly making it somewhat open ended (not as much as CV's granted). So if there is a specific format, let us know. yeah i know.. and decided to use CV's method because it picks up more, you never know i might need more than just a-z and 0-9.. thanks to you both anyway Quote Link to comment Share on other sites More sharing options...
.josh Posted February 23, 2009 Share Posted February 23, 2009 True enough. If the goal is just to get what's between { and }, your solution is fine (had I understood it that way, I would have proposed almost the same thing - see message below). I was under the assumption of a specific format..(I am seeing the examples provided, and I start thinking { something . something . somethingx } and forgot about the no brackets thing.. In your sample, you don't need to escape the { and } characters.. you could simply do this: ~{([^}]+)}~ as these curly braces are already understood / treated as literals. You do have to escape them...{ and } are special chars in the regex. For instance, ~^\w{5,10}$~ // alphanumeric string 5 to 10 chars long edit: well, maybe not inside the neg char class, but outside, yes. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted February 23, 2009 Share Posted February 23, 2009 When you use it as a quantity, as what you have illustrated with \w{5,10} obviously you don't escape them... but outside of that, you still don't need to. By example, If I had to escape { and }, I would not get those curly braces in this example: $str = '{123}'; preg_match('#{\d+}#', $str, $match); // $match[0] = {123} Of course, you can escape them if you want to (and still get the same results). But my point is you don't need to. So in your previous example ~\{([^\}]+)\}~, you have escaped them when there was no need to, as the results are the exact same without escaping those curly braces. Quote Link to comment Share on other sites More sharing options...
.josh Posted February 23, 2009 Share Posted February 23, 2009 eh, you're right. I didn't know that. I assumed it was like the other braces/brackets. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted February 23, 2009 Share Posted February 23, 2009 It's one of those regex gotchas I suppose... 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.