Jump to content

Regular Expressions


jhecht

Recommended Posts

Ok, I've tried everything for this, but I'm seriously just stumped on this one. It's just a simple regular expressions problem I'm sure, but i'm not very good with regular expressions.I need to match text in a specific set of Comment tags. This is used for a template parser im creating simply because i don't like any of the free ones out there (including smarty, it throws issues with javascript in the template). The syntax will look something like this:

HTML will not be looped<!-- FOR loopname -->HTML for this loop that will be repeated<!-- ENDFOR loopname -->HTML will not be looped here either;

I've tried all sorts of things...

<!-- FOR (.*) -->(.*?)<!-- ENDFOR $1 -->//OR<!-- FOR (.*) -->(.*\s?)<!-- ENDFOR $1 -->//OR<!-- FOR (.*) -->([a-z0-9\s\S]+)<!-- ENDFOR $1 -->

And nothing seems to be working to what I wanted. I need for the second regular expression match to be anything, space, non space, word, html, anything at all. However, it doesn't seem to be matching at all (tested using preg_match_all with the /i modifier).It's entirely possible that this is a really simple issue with Regex, but I have no idea.

Link to comment
Share on other sites

Hello there,I have to admit that I have never used reg expressions in php yet but I have experience with them in unix shell scripting with the egrep command.The problem is that I do not understand what you want to match with what >_<If this: (.*?) is a regular expression in php, comparing it with the one used in grep I would have to wonder why you did not add escape characters. Since all 3 symbols are wildcards if used without escape characters.More specifically '.' is any single character. Combined with * we get any '.*' zero or more characters. And the '?' maches the previous atom 0 or 1 times only. So it kind of gets confusing, maybe this is not what you wanted?Of course I might be speaking like a clueless person since I am not regular expressions expert and more specifically since I have never used them before in php. Still, I would like to help, if I can :)

Link to comment
Share on other sites

That was just a random bit of fiddling on my part, perhaps hoping that doing that would do something magical and make this work. On the plus side, i think i got it working, just not as I'd wish. I got it to find the HTML Loop I'm making, but I can't figure out why its given me this error Parse error: syntax error, unexpected T_FOR in C:\apache2triad\htdocs\includes\classes\tmp.php(42) : regexp code on line 1Fatal error: preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: Failed evaluating code: for($i_loop=0;$i_loop < sizeof($this->tplblocks[loop]);$i_loop++){echo " hey you "; } in C:\apache2triad\htdocs\includes\classes\tmp.php on line 42That is the exact error, and yet i can't figure out why. this is the line I'm using

$contents = preg_replace('/<!-- FOR (.*) -->([a-z0-9\s\S]+)<!-- ENDFOR \\1 -->/ie',"for(\$i_\\1=0;\$i_\\1 < sizeof(\$this->tplblocks[loop]);\$i_\\1++){echo \" \\2\"; } ",$contents);

Any help would be greatly appreciated.

Link to comment
Share on other sites

the problem is that u cant use echo there, try instead:$contents = preg_replace('/<!-- FOR (.*) -->([a-z0-9\s\S]+)<!-- ENDFOR \\1 -->/ie',"str_repeat('\\2',sizeof(\$this->tplblocks[loop]));",$contents);if u dun wanna use str_repeat but really for(){}, then make a function that does that and call that instead

Link to comment
Share on other sites

The problem is that the information in the Template will be different for every row, so simply repeating the string is only half the issue. I need for it to loop through the HTML, and any template variables that may be in there as well. I'll see if i can figure it out, I'll reply after i get a bit more work on this.

Link to comment
Share on other sites

I still have no idea how the heck im supposed to get this done. For some reason I get the same error over and over, no matter how i try to fix it. nothing i do works, including splitting up the PHP and regex into multiple things, and then evaluating. I'm really, really lost on this one.

Link to comment
Share on other sites

Why don't you preg_match_all() first then loop through the results, replacing with the modified text?

Link to comment
Share on other sites

tried that, still doesn't work. I don't understand why, but the eval statement just doesn't seem to like <?php ?>.

$string = 'echo "hi"; ';eval($string);//Works$string = '<?php echo "hi"; ?>';eval($string);//Doesn't work

only problem with that is that since this is a parser, I'm going to need those <?php and ?> to be around... Bug perhaps? I doubt it though, because i have phpBB3 and its parser uses the same method of replacing template variables with <?php phpcode ?> instances, and it works fine. Perhaps maybe phpBB 3 goes about it differently, knowing the issues of the eval command, and yet i see a lot of eval commands inside the template and template_compile classes... anyone, anything? I'm feeling really @_@ as of right now.

Link to comment
Share on other sites

Well i got a fix for it as of now. I have to dump the information into a temporary file to be included within the parser after the data has been populated. It's annoying, and I was really hoping to not have to do this, but i guess some things can't be helped. I'd love to use the phpBB3 parser, but its method of caching is really, really annoying.

Link to comment
Share on other sites

Well i got a fix for it as of now. I have to dump the information into a temporary file to be included within the parser after the data has been populated. It's annoying, and I was really hoping to not have to do this, but i guess some things can't be helped. I'd love to use the phpBB3 parser, but its method of caching is really, really annoying.
Why don't you want to include the data in a temporary file? That's how most of the PHP templating engines (including Smarty) work.Using a temporary file is much faster than calling eval().If you really want to eval the compiled template data, then you must use eval('?>'.$contents); rather than just eval($contents)And as for the Regular Expressions:
$contents = preg_replace('/<!-- FOR ([a-z]+) -->(.*?)<!-- ENDFOR \\1 -->/si',	"<?php for(\$i_\\1=0;\$i_\\1 < sizeof(\$this->tplblocks['\\1']);\$i_\\1++):?>\\2<?php endforeach;?>",$template_data);

Works for me

Link to comment
Share on other sites

The main reason i don't like the Smarty template, and the new phpBB3 template is due to the caching. It annoys me beyond all reason. And I can get the string to be replaced in, thats not the problem. Adding in the e modifier to the preg_replace regex is what causes the problems. Even simply replacing the string and calling eval gives me the same error. For now i just have the template write the file, include it, and on the next page it deletes all of the files in the row that end with the predetermined extension. Btw, this is for a forum board I've called Furm. Read up on it at http://furm.wordpress.com/

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...