如何在以下代码中插入逗号

时间:2014-10-02 19:27:43

标签: php html css

通过以下代码获取HTML标记列表:

代码

<?php
// Heavy testing:

ini_set('memory_limit', '400M');

$doc = new DOMDocument();
$doc->loadHTML(file_get_contents('index.php')); // Don't know how to make it use the loaded document

$xpath = new DOMXpath($doc);
$nodes = $xpath->query('//*');

$names = array();
foreach ($nodes as $node) {
    $names[] = $node->nodeName;
}

echo join(PHP_EOL, array_unique($names));
// Source: https://gist.github.com/kwoodfriend/9669711

?>

输出:

html
body
p
title
link
meta
div
article
h1
b
strong
br
hr
h2
ul
li
ol
s
cite
a
h3

但它需要在标记之间添加逗号(,)。

我有以下代码:

<?php
$data = file_get_contents("$names"); //read the file
$convert = explode("\n", $data); //create array separate by new line

for ($i=0;$i<count($convert);$i++) 
{
    echo $convert[$i].', '; //write value by index
}
// Source: https://php.net/manual/it/function.file-get-contents.php#102319
?>

只是在行尾添加逗号。

所需的结果是从body及以下开始的每个标签。(不是同一标签的两倍:p,div,p等......)

   p, div, article, h1, b, strong, br, hr, h2, ul, li, ol, s, cite, a, h3 {

    }

如您所见,我还需要在CSS{之间添加一些}代码。

2 个答案:

答案 0 :(得分:1)

这个答案考虑了以下几点:

  • 跳过html和正文
  • 为每个元素插入逗号和空格
  • 添加css大括号和$ css var

foreach ($nodes as $node) {

    // skip html and body
    if($node->nodeName === 'html' or $node->nodeName === 'body') {
        continue;
    }

    // insert everything else
    $names[] = $node->nodeName;
}

$css = 'color:red;';

echo join(', ', array_unique($names)) . " {\n" . $css . "\n}\n";

如果您有许多节点元素,请优化查询$nodes = $xpath->query('//*'); 或过滤/跳过节点!我在这里使用array_unique(),因此每个节点只有一次被压缩。


完全适合你的事情:)

<?php

// html
$content = '<!DOCTYPE html>
<html lang="en" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Welcome to WPИ-XM Serverpack!</title>
    <link rel="icon" href="tools/webinterface/favicon.ico" type="image/x-icon" />
    <meta http-equiv="refresh" content="3; URL=tools/webinterface/">
</head>
<body bgcolor="E7E7E7" text="333333">
<div id="container">
    <div id="content">
        <h1>Welcome to the WPИ-XM server stack!</h1>
    </div>
    <strong>You should be redirected to the administration interface of WPN-XM in 5 seconds.</strong>
    <br>
     Click <a href="tools/webinterface/">here</a> for immediate redirection.
</div>
</body>
</html>';

$doc = new DOMDocument();
$doc->loadHTML($content);

$xpath = new DOMXpath($doc);
$nodes = $xpath->query('//*');

// ---- my answer ----

foreach ($nodes as $node) {

    // skip html and body
    if($node->nodeName === 'html' or $node->nodeName === 'body') {
        continue;
    }

    // insert everything else
    $names[] = $node->nodeName;
}

$css = 'color:red;';

echo join(', ', array_unique($names)) . " {\n" . $css . "\n}\n";

输出:

head, meta, title, link, div, h1, strong, br, a {
color:red;
}

直播@ http://ideone.com/pMLgAC

答案 1 :(得分:0)

为什么不直接破坏阵列?

$tag_string = implode(', ', $names);
echo $tag_string;