Need Help Converting to an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ChibiGuy
Forum Newbie
Posts: 22
Joined: Fri Jul 11, 2003 11:23 pm
Location: USA
Contact:

Need Help Converting to an array

Post by ChibiGuy »

I have a template which holds this data:

Code: Select all

<HTML>
<HEAD>
<TITLE>SMTemplates V1.00</TITLE>
<STYLE>
 td,body&#123;font-family:tahoma; font-size: 11px; color:black&#125;
</STYLE>
</HEAD>
<BODY>
<b>This file demonstrates the use of SMTemplates.</b><br><br>
This is header.tpl<br>
 &#123;&#123;ID1&#125;&#125;<br>
 &#123;&#123;ID2&#125;&#125;<br>
 <!-- BEGIN SUBBLOCK -->
    <!-- BEGIN SUBBLOCK1 -->
        IM IN SUBBLOCK1
    <!-- END SUBBLOCK1 -->
    IM IN SUBBLOCK<br>
 <!-- END SUBBLOCK -->
Is there any possible way to store each and every one of those blocks in an array?
i.e. given the template above, i should wind up with this array

Code: Select all

TEMPLATE = array(
    &#1111;MAIN] = <HTML>
                  <HEAD>
                  <TITLE>SMTemplates V1.00</TITLE>
                  <STYLE>
                  td,body&#123;font-family:tahoma; font-size: 11px; color:black&#125;
                  </STYLE>
                  </HEAD>
                  <BODY>
                  <b>This file demonstrates the use of SMTemplates.</b><br><br>
                  This is header.tpl<br>
                  &#123;&#123;ID1&#125;&#125;<br>
                  &#123;&#123;ID2&#125;&#125;<br>
    &#1111;SUBBLOCK] = array (
        &#1111;SUBBLOCK1] = array (
            &#1111;0] = IM IN SUBBLOCK1
            )
        &#1111;1] = IM IN SUBBLOCK<br>
    )    
)
Last edited by ChibiGuy on Sat Jul 12, 2003 7:41 pm, edited 1 time in total.
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post by trollll »

Sure. If you read in the file into an array (with file("filename")) and then cycle through each element of the array you can construct the logic within the loop to fit each piece into the element of a new array that fits your requirements. XML would probably work a bit better as a source file for handling something like this, but well-structured HTML could work just as well.
ChibiGuy
Forum Newbie
Posts: 22
Joined: Fri Jul 11, 2003 11:23 pm
Location: USA
Contact:

Post by ChibiGuy »

Could you post some example code? Im not sure how to do that.
ChibiGuy
Forum Newbie
Posts: 22
Joined: Fri Jul 11, 2003 11:23 pm
Location: USA
Contact:

Post by ChibiGuy »

nm I've figured it out!

Code: Select all

$linecount = count($lines = explode("\n", $contents));
		for($i = 0; $i <= $linecount; $i++ ) &#123;
			preg_match("<!-- BEGIN (.*) -->", $lines&#1111;$i], $m);
					
			if( $m || ($OPEN && $OPEN != '$BLOCKS') ) &#123;
				
				// If we find a new <!-- BEGIN &#1111;NAME] -->, then: 
				if( $m && $OPEN ) &#123;
					// A BLOCK IS ALREADY OPENED & AN INNERBLOCK IS FOUND
					// NOW APPEND TO $OPEN AND eval() IT.
					$OPEN .= '&#1111;'.$m&#1111;1].']';
					$BLOCKS&#1111;MAIN] .= "<TPL BLOCK="".$OPEN."">\n";
					//eval("$OPEN .= \$lines&#1111;\$i];");
					
					print "$OPEN = $lines&#1111;$i]<BR>\n";

				&#125; elseif( $m && !$OPEN ) &#123;
					// A PARENT BLOCK IS FOUND ($OPEN DOESN"T EXIST
					// AND <!-- BEGIN &#1111;NAME] --> WAS FOUND).
					// NOW CREATE $OPEN AND eval() it.
					$OPEN = '$BLOCKS&#1111;'.$m&#1111;1].']';
					$BLOCKS&#1111;MAIN] .= "<TPL BLOCK="".$OPEN."">\n";
					//eval("$OPEN = \$lines&#1111;\$i];");
					
					print "$OPEN = $lines&#1111;$i]<BR>\n";
				
				&#125; elseif( !$m && $OPEN ) &#123;
					// A BLOCK IS OPEN AND THERE IS NO NEW BLOCK FOUND, 
					// SO JUST APPEND $lines&#1111;$i] TO CURRENT WORKING BLOCK
					//eval("$OPEN .= \$lines&#1111;\$i];");
					
					print "$OPEN = $lines&#1111;$i]<BR>\n";
				&#125;
				
				if( ( ($m && $OPEN) || ($m && !$OPEN) || (!$m && $OPEN) ) && preg_match("<!-- END (.*) -->", $lines&#1111;$i], $n) ) &#123;
					// WE FOUND A <!-- END &#1111;NAME] -->, SO CLOSE WORKING BLOCK
					$OPEN = preg_replace("/(.+)(\&#1111;.+\])&#123;1&#125;$/", "\\1", $OPEN);
				&#125;
				
			&#125; else &#123;
			
				// NO WORKING BLOCK, SO APPEND THE CURRENT LINE TO $BLOCK&#1111;MAIN]
				$BLOCKS&#1111;MAIN] .= $lines&#1111;$i]."\n";
				
			&#125;
				
		&#125;
Post Reply