如何生成 HTML 文件取决于数组长度 php


How to generate HTML files depends on array length php

如何生成HTML文件取决于数组长度php

我有一个 XML 文件。

<books>
  <book x="1" y="2">
     <name x="5" y="12">Java</name>
     <author x="8" y="16">Rao</author>
   </book>
   <book x="12" y="20">
     <name x="5" y="12">Php</name>
     <author x="5" y="12">Naidu</author>
   </book>
   <book x="19" y="29">
     <name x="25" y="22">Xml</name>
     <author x="25" y="12">Gowda</author>
   </book>
</books>

我已经使用 php 将其转换为数组。

我有标准的html文件,即模板。

<body>
  <div id="books" style="float:right; position:absolute; left: 199px; top: 245px;">
    <div id="book" style="float:right; position:absolute; left: 199px; top: 245px;">
    <div id="name" style="float:right; position:absolute; left: 199px; top: 245px;"></div>
    <div id="author" style="float:right; position:absolute; left: 199px; top: 245px;"></div>
     </div>
   </div>
</body>

根据数组legth,我需要在div中生成许多具有动态值的html文件,这些文件存在于数组中。在这里我需要生成 3 个 html 文件(因为我有 3 个书籍元素)。如何使用数组生成 html 文件。

数组如下所示:

Array
(
    [book] => Array
        (
            [0] => Array
                (
                    [name] => Java
                    [author] => Rao
                    [@attributes] => Array
                        (
                            [x] => 1
                            [y] => 2
                        )
                )
            [1] => Array
                (
                    [name] => Php
                    [author] => Naidu
                    [@attributes] => Array
                        (
                            [x] => 12
                            [y] => 20
                        )
                )
            [2] => Array
                (
                    [name] => Xml
                    [author] => Gowda
                    [@attributes] => Array
                        (
                            [x] => 19
                            [y] => 29
                        )
                )
        )
)

在您的 html 模板中,使用像 real-template 这样的分隔符:

<body>
    <div id="books" style="float:right; position:absolute; left: 199px; top: 245px;">
        <div id="book" style="float:right; position:absolute; left: 199px; top: 245px;">
            <div id="name" style="float:right; position:absolute; left: 199px; top: 245px;">{name}</div>
            <div id="author" style="float:right; position:absolute; left: 199px; top: 245px;">{author}</div>
        </div>
    </div>
</body>

在 PHP 端,使用 regexp 替换模板中的数据:

        $html_template = file_get_contents('/path/to/your/html/template');
        foreach($books['book'] as $i => $book) {
            $file_name = "html_page_for_book_{$i}";
            $html_file = fopen($file_name,'w');
            $html_data = preg_replace(array('/{author}/','/{name}/'),array($book['author'],$book['name']),$html_template);
            fwrite($html_file,$html_data);
            fclose($html_file);
        }
我希望

可以将XML解析为数组的人也可以做到这一点,但无论如何你都会去:

<body>
    <div id="books" style="float:right; position:absolute; left: 199px; top: 245px;">
        <?php foreach($books as $book) : ?>
        <div id="book" style="float:right; position:absolute; left: <?php echo $book['@attributes']['x']; ?>px; top: <?php echo $book['@attributes']['y']; ?>px;">
            <div id="name" style="float:right; position:absolute; left: 199px; top: 245px;"><?php echo $book['name']; ?></div>
            <div id="author" style="float:right; position:absolute; left: 199px; top: 245px;"><?php echo $book['author']; ?></div>
        </div>
        <?php endforeach; ?>
    </div>
</body>

教育|信息技术我为这本书添加了 x/y 属性,我相信你可以自己完成剩下的工作