Jump to content

Recommended Posts

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.

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/236399-stuck-on-somthing-simple/
Share on other sites

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
}

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.

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.

 

 

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!!

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.