Forum Moderators: coopster

Message Too Old, No Replies

Loading all html comments into array

         

Nutter

11:14 pm on Jan 3, 2006 (gmt 0)

10+ Year Member



I'm working on a site that'll be using templates. In these templates, there will be user variables that are inside of html comments. So, <!--companyname--> will change to "Bob's Super Dooper Store" when the template is parsed.

Problem is, some of the variables may involve quite a bit of work to get the right value; and I don't want the server to get tied up running functions that aren't going to get used.

So, my thinking is to do a

str_replace("<!--companyname-->", function_to_get_name, $output); 
only if <!--companyname--> is there.

What I'd like to do is to load all of the comments into an array, so that I can just do "if in_array();" then do the replacement. Problem is, I can't figure out how to parse all the comments into an array.

How's that for a long, drawn out question? :-)

ergophobe

4:56 pm on Jan 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



preg_match() [php.net]

Hows that for a non-drawn out answer ;-)

Nutter

5:59 pm on Jan 4, 2006 (gmt 0)

10+ Year Member



I was afraid you were going to suggest regex. Never spent the time to figure it out. Guess it's about time :-)

So...

$mystring = "yada, yado <!--tagnumber1--> some more stuff <!--anothercode--> and some more";
$return = preg_match("/^(<!--)?(-->)/i", $mystring, $matches);

Would make...
$matches[0] = "tagnumber1";
$matches[1] = "anothercode";

Look about right?

ergophobe

8:19 pm on Jan 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Actually, not quite.

1. The $matches[0] contains the full string matched, so your results should start with $mathces[1] -- do a print_r($matches)

2. You need to make your pattern ungreedy and caputre the contents of the comment in parentheses, not the comment delimiters themselves

$pattern = '/^<!--(.*)-->/iU';