douyiqi9640 2014-10-02 19:27
浏览 73
已采纳

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

Am getting a list of HTML tags by the code below:

Code:

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

?>

Output:

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

But it needs to add a comma(,) in between tags.

I have the following code:

<?php
$data = file_get_contents("$names"); //read the file
$convert = explode("
", $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
?>

That just adds the comma at the end of the line.

The desired result is every tag starting body and below.(not double of the same tag: p, div, p, etc...)

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

    }

As you can see, i also need to add some CSS code in between of { and }.

  • 写回答

2条回答 默认 最新

  • douyouqian8550 2014-10-02 19:38
    关注

    This answer takes the following things into account:

    • skip html and body
    • insert comma and a space for every element
    • add css braces and $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)) . " {
    " . $css . "
    }
    ";
    

    If you have to many node elements, refine your query $nodes = $xpath->query('//*'); or filter/skip nodes! I'm using array_unique() here, so that every node is only once adressed.


    Just for you the full thing :)

    <?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)) . " {
    " . $css . "
    }
    ";
    

    Output:

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

    Live @ http://ideone.com/pMLgAC

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料