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

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

问题描述:

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

通过以下代码获取HTML标签列表: p>

代码 strong>: p>

 &lt;?php 
 //重度测试:
 
ini_set('memory_limit','400M'); 
 \  n $ doc = new DOMDocument(); 
 $ doc-&gt; loadHTML(file_get_contents('index.php'));  //不知道如何使用加载的文档
 
 $ xpath = new DOMXpath($ doc); 
 $ nodes = $ xpath-&gt; query('// *'); 
 \  n $ names = array(); 
foreach($ nodes as $ node){
 $ names [] = $ node-&gt; nodeName; 
} 
 
echo join(PHP_EOL,array_unique($ names));  
 //来源:https://gist.github.com/kwoodfriend/9669711

?&gt; 
  code>  pre> 
 
 

输出: p> \ ñ

  HTML 
body \ NP 
标题\ NLINK 
meta \ NDIV 
article \ NH1 \ NB 
strong \ NBR \ NHR \ NH2 \ NUL \ NLI \ NOL \纳秒
cite \呐\ NH3  
  code>  pre> 
 
 

但它需要在标签之间添加逗号(, code>)。 p>

I 具有以下代码: p>

 &lt;?php 
 $ data = file_get_contents(“$ names”);  //读取文件
 $ convert = explode(“
”,$ data);  //按新行分隔创建数组
 
for($ i = 0; $ i&lt; count($ convert); $ i ++)
 {
 echo $ convert [$ i]。',';;  //按索引写入值
} 
 //来源:https://php.net/manual/it/function.file-get-contents.php#102319
?&gt; 
  code>  
 
 

这只是在行尾添加逗号。 p>

所需的结果是每个标记都以 body code>开头 。(不是同一标签的两倍:p,div,p等......) p>

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

如您所见,我还需要添加一些 { code>和} code>之间的代码> CSS code>代码。 p> div>

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

Why don't you just implode the array?

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