A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearhow to get string between [LOOP]…[/LOOP] - page 1
User InfoPosts
how to get string between [LOOP]…[/LOOP]#1
how to get string between [LOOP]...[/LOOP]
i try this but not work

$gloop = preg_replace((/\[LOOP\]([^\[]*)\[\/LOOP\]/(, (\\1(, $var);


...
string:

(<tr>
[LOOP]<th scope="col">%Gdata_All%</th>[/LOOP]
</tr>(


but i would string in [LOOP]...[/LOOP]

posted date: 2009-04-12 16:51:00


Re: how to get string between [LOOP]…[/LOOP]#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-12 16:51:01


Re: how to get string between [LOOP]…[/LOOP]#3
I would first like to say, are you creating your own template library? There are some mature ones around like Smarty which should be able to fill your requirements. This article by Jeff Atwood is also good read : "Don(t Reinvent The Wheel, Unless You Plan on Learning More About Wheels"If you want to roll your own solution then that is fine, here are some suggestions.BBCodeThis method relies on having the BBCode extension installed with PHP, it is native and does not rely on the regular expression engine (slow). This solution will probably be the fastest.You can have the BBCode parser convert [LOOP]<td><?php print $row->title; ?></td>[/LOOP]into<?php foreach($result as $row): ?><td><?php print $row->title; ?></td><?php endforeach; ?>Look into the BBCode arguments to learn about passing variables into your [LOOP] syntax.You can view some more examples on the bbcode_create method page.Regular ExpressionsThey are a little bit slower, but are enabled by default on all PHP installations. Credits to go Gumbo for this regular expression (You should up-vote him)./\[LOOP]((?:[^[]+|\[(?=\/LOOP]))*)\[\/LOOP]/You can then use it as follows.if (preg_match((%\[LOOP\]((?:[^[]+|\[(?=/LOOP\]))*)\[/LOOP\]%i(, $subject, $result)) { # Successful match # You can access the content using $result[1];} else { # Match attempt failed}-Mathew

posted date: 2009-04-12 17:31:00


Re: how to get string between [LOOP]…[/LOOP]#4
With (.*?) it would still match "[LOOP]...[LOOP]...[/LOOP]". I think the OP had that part right: ([^]]*). Assuming, of course, there can't be any brackets other than those in the tags. But that's a side issue; the real problem lay in trying to use preg_replace instead of preg_match. +1

posted date: 2009-04-12 22:04:00


Re: how to get string between [LOOP]…[/LOOP]#5
I would use this regular expression:/\[LOOP]((?:[^[]+|\[(?=\/LOOP]))*)\[\/LOOP]/It matches a sequence of any character other than [ ([^[]+) or the [ that is not followed by a /LOOP] (\[(?!\/LOOP]), (?!expr) is a negative look-ahead assertion) between a [LOOP] and [/LOOP]; the surrounding (?:expr) is a non-capturing grouping.But it would certainly be better if you use a real parser to detect malformed code as The Pixel Developer already pointed out.

posted date: 2009-04-13 06:02:00


Re: how to get string between [LOOP]…[/LOOP]#6
Ah yes, good point. The regex posted by Gumbo below solves this issue.

posted date: 2009-04-13 10:45:00


Re: how to get string between [LOOP]…[/LOOP]#7
Please have a look at a shortcode / BBCode library I authored: github.com/thunderer/Shortcode . It makes such things way easier to do.

posted date: 2015-05-11 05:37:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.