SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    array (tree) from html

    There is a HTML, with defined special tags <tmpl></tmpl>
    Code:
     
    ....
    <table>
    <tmpl:ROW>
    <tr><td>
    	{var1}
    	
    	<tmpl:admin>
    	<tr><td>{var2}</td></tr>
    		<tmpl:sub_admin>
    		<tr><td>{var2}</td></tr>
    		</tmpl:sub_admin>
    	</tmpl:admin>
    
    	<tmpl:admin1>
    	<tr><td>{var2}</td></tr>
    	</tmpl:admin1>
    
    </td></tr>
    </tmpl:ROW>
    </table>
    .....
    How can I make an array like this
    Code:
    [blocks] => Array
    	(
    	[ROW] => Array
    		(
    		[admin] => array
    			(
    			[0] => sub_admin
    			)
    		[1] => admin1
    		)
    	)

  2. #2
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this seems to do what you want (using your HTML). let me know if you need an explanation of any of it. :-)

    PHP Code:
    <?php

    $test 
    '<table>
    <tmpl:ROW>
    <tr><td>
        {var1}
        
        <tmpl:admin>
        <tr><td>{var2}</td></tr>
            <tmpl:sub_admin>
            <tr><td>{var2}</td></tr>
            </tmpl:sub_admin>
        </tmpl:admin>

        <tmpl:admin1>
        <tr><td>{var2}</td></tr>
        </tmpl:admin1>

    </td></tr>
    </tmpl:ROW>
    </table>'
    ;

    $blocks get_tree($test);

    echo 
    '<pre>';
    print_r($blocks);
    echo 
    '</pre>';


    function 
    get_tree($html)
    {
        
    $tree = array();

        
    preg_match_all('#<tmpl:(.+?)>(.+?)</tmpl:\1>#is'$html$matches);

        for (
    $i 0$x count($matches[1]); $i $x; ++$i)
        {
            
    $sub_tree get_tree($matches[2][$i]);

            if (
    count($sub_tree))
                { 
    $tree[$matches[1][$i]] = $sub_tree; }
            else
                { 
    $tree[count($tree)] = $matches[1][$i]; }
        }

        return 
    $tree;
    }

    ?>
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. That's what I need.
    Some explanation - would be nice.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •