doxd96148 2014-05-05 06:51
浏览 47

在php中制作css帧生成器

i have tried to make a css frame generator like this one i have also found a same answer here which working fine except one thing

PHP Code:

<?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 . " {}
";
        if ($parentTag->nodeName == 'a') {
            foreach (array("link", "visited", "hover", "active", "focus") as $pseudo)
                echo $my_css . ':' . $pseudo . " {}
";
        }
        $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);
}
?>

Output:

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 {}

if i put below code in source i got this error

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

here is the code which i tried:

<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>

i have tried to solve but cant

  • 写回答

1条回答 默认 最新

  • dtrj74376 2014-05-05 07:08
    关注

    The problem is with your string generation in PHP.

    As you can see here in the documentation if you enclose your strings in double quotes " you must escape all double quotes inside that string, for them to be treated as literal "s, otherwise PHP will think your string has ended and go back to trying to parse PHP, likewise if you use single quotes '.

    The code you tried to change it to is littered with a mix of ' and ", so pick one and use that in your PHP string creation being sure to escape any instances of it you encounter inside your string.

    They both have their usages, read the documentation I linked above to help decide which one best fits your needs.

    E.g.

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

    You are trying to do this:

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

    Why? Because PHP thinks the string ends after "Alex" when the quotes end (StackOverflows syntax highlighting also shows that nicely).

    Edit: For your reference, how I came to this conclusion from your error message:

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

    This is saying it was trying to parse PHP and hit the character 1 which it wasn't expecting. If you look in your modified source on this line

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

    As you can see here the 1 is following a ', which also happens to be what you're surrounding your strings in.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法