php - 在 php 中制作 css 框架生成器

标签 php html css

我尝试制作一个像this one 这样的css 框架生成器。 我也在这里找到了一个相同的答案,除了一件事外工作正常

PHP代码:

<?php

function print_css($parentTag, $prefix, &$dup_checker) {
    if ($parentTag->nodeName == '#text') {
        return;
    }
    if ($parentTag->hasAttribute("class") || $parentTag->hasAttribute("id")) {
        $idpart = ($parentTag->hasAttribute("id")) ? "#" . $parentTag->getAttribute("id") : "";
        $classpart = ($parentTag->hasAttribute("class")) ? "." . str_replace(" ", ".", $parentTag->getAttribute("class")) : "";
        $my_css = $prefix . $parentTag->nodeName . $idpart . $classpart;
    } else {
        $my_css = $prefix . $parentTag->nodeName;
    }

    if (!isset($dup_checker[$my_css])) {
        echo $my_css . " {}\n";
        if ($parentTag->nodeName == 'a') {
            foreach (array("link", "visited", "hover", "active", "focus") as $pseudo)
                echo $my_css . ':' . $pseudo . " {}\n";
        }
        $dup_checker[$my_css] = 1;
    }

    $nodes = $parentTag->childNodes;
    for ($i = 0; $i < $nodes->length; $i++) {
        $node = $nodes->item($i);
        print_css($node, $my_css . ' ', $dup_checker);
    }
}

header('Content-type: text/plain');
$source = '<body class="theme">
    <div class="header class2">
        <h1><a href="#">Welcome</a></h1>
    </div>
    <div class="content">
        <div class="sidebar">
            <ul>
                <li><a href="#">Link 1</a></li>
                <li id="hellos" class="meow wuff"><a href="#"><span>Link 2</span></a></li>
                <li><a href="#">Link 3</a></li>
                <li><a href="#">Link 4</a></li>
                <li><a href="#">Link 5</a></li>
            </ul>
        </div>
        <div class="main">
            <h2>Main Heading</h2>
            <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
        </div>
    </div>
    <div class="footer">
        <p id="hello">Copyright &copy; 2012 Me!</p>
    </div>
</body>';
$html = new DOMDocument;
$html->loadHTML($source);
$nodes = $html->getElementsByTagName("body");
$css = "";
$dup_checker = array();
for ($i = 0; $i < $nodes->length; $i++) {
    $node = $nodes->item($i);
    print_css($node, '', $dup_checker);
}
?>

输出:

body.theme {}
body.theme div.header.class2 {}
body.theme div.header.class2 h1 {}
body.theme div.header.class2 h1 a {}
body.theme div.header.class2 h1 a:link {}
body.theme div.header.class2 h1 a:visited {}
body.theme div.header.class2 h1 a:hover {}
body.theme div.header.class2 h1 a:active {}
body.theme div.header.class2 h1 a:focus {}
body.theme div.content {}
body.theme div.content div.sidebar {}
body.theme div.content div.sidebar ul {}
body.theme div.content div.sidebar ul li {}
body.theme div.content div.sidebar ul li a {}
body.theme div.content div.sidebar ul li a:link {}
body.theme div.content div.sidebar ul li a:visited {}
body.theme div.content div.sidebar ul li a:hover {}
body.theme div.content div.sidebar ul li a:active {}
body.theme div.content div.sidebar ul li a:focus {}
body.theme div.content div.sidebar ul li#hellos.meow.wuff {}
body.theme div.content div.sidebar ul li#hellos.meow.wuff a {}
body.theme div.content div.sidebar ul li#hellos.meow.wuff a:link {}
body.theme div.content div.sidebar ul li#hellos.meow.wuff a:visited {}
body.theme div.content div.sidebar ul li#hellos.meow.wuff a:hover {}
body.theme div.content div.sidebar ul li#hellos.meow.wuff a:active {}
body.theme div.content div.sidebar ul li#hellos.meow.wuff a:focus {}
body.theme div.content div.sidebar ul li#hellos.meow.wuff a span {}
body.theme div.content div.main {}
body.theme div.content div.main h2 {}
body.theme div.content div.main p {}
body.theme div.footer {}
body.theme div.footer p#hello {}

如果我把下面的代码放在源代码中,我会得到这个错误

Parse error: syntax error, unexpected '1' (T_LNUMBER) in E:\wamp\www\ajaxtest\all ajax file\xhtml generator\index.php on line 35

这是我试过的代码:

<nav>
            <ul class="navigation">
                <li data-slide='1'><a href="#">slide1</a></li>
                <li data-slide='2'><a href="#">slide2</a></li>
                <li data-slide='3'><a href="#">slide3</a></li>
                <li data-slide='4'><a href="#">slide4</a></li>
            </ul>
        </nav>
        <div class="main">
            <div class='slide' id='slide1' data-slide='1' data-stellar-background-ratio='0.5'>
                <div class="container">
                    <div class="row">
                        <div class="col-md-6 slide-1-img">
                            <img src="" alt="" class="logoimg" />
                        </div>
                        <div class="col-md-6">
                            <h1>welcome to my new sexy parallax website</h1>
                        </div>
                    </div>
                </div>
                <a href="" data-slide='2' title=""></a>
            </div>
            <div class='slide' id='slide2' data-slide='2' data-stellar-background-ratio='0.5'>

                <a href="" data-slide='3' title=""></a>
            </div>
            <div class='slide' id='slide3' data-slide='3' data-stellar-background-ratio='0.5'>

                <a href="" data-slide='4' title=""></a>
            </div>
            <div class='slide' id='slide1' data-slide='1' data-stellar-background-ratio='0.5'>

            </div>
        </div>

我已经尝试解决但不能

最佳答案

问题在于您在 PHP 中生成的字符串。

如你所见here in the documentation如果您将字符串用双引号括起来 " 您必须转义该字符串中的所有双引号,以便将它们视为文字 ",否则 PHP 会认为您的字符串具有结束并返回尝试解析 PHP,同样,如果您使用单引号 '

您尝试将其更改为的代码混杂着 '"因此选择一个并在您的 PHP 字符串创建中使用它确保转义您在字符串中遇到的任何实例。

它们都有自己的用法,请阅读我上面链接的文档,以帮助确定哪一个最适合您的需求。

例如

echo "Alex's test string"; // This is ok
echo 'Alex\'s test string'; // This is ok as well (the apostrophe is escaped)

您正在尝试这样做:

echo 'Alex's test string'; // This will error

为什么?因为当引号结束时,PHP 认为字符串在“Alex”之后结束(StackOverflows 语法突出显示也很好地显示了这一点)。

编辑:供您引用,我是如何从您的错误消息中得出这个结论的:

Parse error: syntax error, unexpected '1' (T_LNUMBER) in E:\wamp\www\ajaxtest\all ajax file\xhtml generator\index.php on line 35

这表示它正在尝试解析 PHP 并命中它不期望的字符 1。如果您在此行查看修改后的源代码

<li data-slide='1'><a href="#">slide1</a></li>
                ^

正如您在这里看到的,1 跟在 ' 之后,这也恰好是您包围字符串的内容。

关于php - 在 php 中制作 css 框架生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23466521/

相关文章:

html - z-index 在我的菜单上不起作用

html - 你如何获得位置 :absolute; element above a position:relative; one

html - 如何在不影响文本的情况下设置背景不透明度

php - 如何获取变体的库存数量 woocommerce

php - 当出现一些依赖错误时,如何停止在 Laravel 中执行 Create 操作

css - margin-top : auto and margin-bottom: auto amounting to 0 margin? 背后的基本原理是什么

html - 将两行分隔的文本放在一个内联 div 中

javascript - 查询/CSS : Full size background image

php - Mysqli 预处理语句 : several WHERE clauses and WHERE IN (Array)

php - 在 Codeigniter 中显示图像