klyxmaster Posted May 14, 2011 Share Posted May 14, 2011 I finished a template engine, its tiny, needed it for a small project. so with that in mind I am stuck on adding a new section that is not going well. tag: {subgame} this will show the layout of a particular game - works fine yippe! but that is just for one game. so now I am changing it to show more than one, and I cannot use that tag over, due to the way "str_replace" works (changes all in one swoop) so I thought doing it this way 2nd Method: {subgame1} {subgame2} {subgame3} etc... the search is: where field is one of the triggers (in this case SUBGAME) if (strstr( '{'.$field.'}",$html_str) ) works fine for the first one, but obviously ignores the 2nd method I tried preg_match - but i know i am missing something, because I cannot get it to work. and ideal way would be the fnmatch( as of 5.3 its available for windows now) but only supports up to 260 characters - not good for website So what I am looking for: psuedo code: if ( '{'.$field.'[optional digits]}' ) { do code here } I know preg_match seems to be the way but cant get it to see the numbers and if it does, it misses the origianl game, or worse crashes. Quote Link to comment https://forums.phpfreaks.com/topic/236399-stuck-on-somthing-simple/ Share on other sites More sharing options...
Zurev Posted May 14, 2011 Share Posted May 14, 2011 Just make one template file for each subgame, and loop through that template file and display it once for each subgame. I believe that's how software like phpbb does it when they use these sort of templating systems. Quote Link to comment https://forums.phpfreaks.com/topic/236399-stuck-on-somthing-simple/#findComment-1215344 Share on other sites More sharing options...
wildteen88 Posted May 14, 2011 Share Posted May 14, 2011 You're right in thinking preg_match is what you need. You can use the following regex to match anything within { and } ~{[a-z0-9_\- ]+}~ Example code $field = 'my Game'; $text = "{subgame1} {subgame} {{$field}} {subgame12}"; echo $text; preg_match_all('~{[a-z0-9_ -]+}~i', $text, $matches); echo '<pre>' . print_r($matches, true) . '</pre>'; You can then use a foreach loop to run your code for each match foreach($matches[0] as $gameMatch) { // run code here } Quote Link to comment https://forums.phpfreaks.com/topic/236399-stuck-on-somthing-simple/#findComment-1215346 Share on other sites More sharing options...
Zurev Posted May 14, 2011 Share Posted May 14, 2011 I mean if you think about it, the {replace} template systems are not amazing, but they work pretty well. However you should only have to modify your template files if you want to change the look of something. This system, if you added a new game, you would have to add another {game646}. Parsing all that template file with file_get_contents (assuming) is a bit of overhead, adding regular expressions into it is going even farther. If you just loop through a single template called something like 'eachSubgame.tpl' and display it once for each subgame, it would definitely be better. Quote Link to comment https://forums.phpfreaks.com/topic/236399-stuck-on-somthing-simple/#findComment-1215355 Share on other sites More sharing options...
klyxmaster Posted May 14, 2011 Author Share Posted May 14, 2011 You're right in thinking preg_match is what you need. You can use the following regex to match anything within { and } ~{[a-z0-9_\- ]+}~ Example code $field = 'my Game'; $text = "{subgame1} {subgame} {{$field}} {subgame12}"; echo $text; preg_match_all('~{[a-z0-9_ -]+}~i', $text, $matches); echo '<pre>' . print_r($matches, true) . '</pre>'; You can then use a foreach loop to run your code for each match foreach($matches[0] as $gameM ok I like your suggestion (at least i know im going in the right direction) so I went looking at the preg_match here is what I have so far: btw: $html_file is from $html_file = file( ) $field is an array of predefined tags. [code]if( preg_match("/{".$field."[1-9]?}/",$html_file) )[/code] now the page displays correctly and parses all the defined tags just fine, but still will not act on the SUBGAMEx so in the "case" section I added this [code]case preg_match("/{SUBGAME[1-9]?}/",$html_file): [/code] again all other tags are addressed correctly, but still ignores SUBGAMEx Am I still close? or am I way off now :-) ps I forgot to mention is that some of the tags are actually functions. other tags are just replacesments such as SITENAME, AUTHOR etc.. and other tags like GENRE for example call up funtions (to display a bullet list as int he GENRE example) So i have a oneliner to take care of all the replacemnt text and I use a SWITCH to check on triggers that launch a function (prob doing the hardway) that is why there is a if/then check - because that is checking for keywords that are used in functions, once those are checked the program acts on the regular string replacement. Quote Link to comment https://forums.phpfreaks.com/topic/236399-stuck-on-somthing-simple/#findComment-1215408 Share on other sites More sharing options...
klyxmaster Posted May 14, 2011 Author Share Posted May 14, 2011 I mean if you think about it, the {replace} template systems are not amazing, but they work pretty well. However you should only have to modify your template files if you want to change the look of something. This system, if you added a new game, you would have to add another {game646}. Parsing all that template file with file_get_contents (assuming) is a bit of overhead, adding regular expressions into it is going even farther. If you just loop through a single template called something like 'eachSubgame.tpl' and display it once for each subgame, it would definitely be better. THAT is a very interesting approach.. if I cannot get this preg_figured out, I may do that. Just a quick description the basic page has 1 game features displays images, details, downloads vids etc... the subgame are other games similar or related and at the most there will never be more than 9 or 10 shown. thats why I have not taken your approach yet. It does parse a separate html file that has all the imaga, links descriptions etc... once the tag is found. just right now it doesnt see that tag (yet) thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/236399-stuck-on-somthing-simple/#findComment-1215410 Share on other sites More sharing options...
wildteen88 Posted May 14, 2011 Share Posted May 14, 2011 Your using preg_match incorrectly here o in the "case" section I added this case preg_match("/{SUBGAME[1-9]?}/",$html_file): preg_match will return 1 if it found a match and 0 if it didn't. The third parameter for preg_match will return any array of matches. Syntax for preg_match: preg_match(pattern, subject, $matches) NOTE: preg_match will return one match from the pattern. If you have multiple {subgameX}'s you need to use preg_match_all. Quote Link to comment https://forums.phpfreaks.com/topic/236399-stuck-on-somthing-simple/#findComment-1215419 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.