Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php DomDocument在样式元素中从文件插入CSS_Php_Css_Inline_Domdocument - Fatal编程技术网

Php DomDocument在样式元素中从文件插入CSS

Php DomDocument在样式元素中从文件插入CSS,php,css,inline,domdocument,Php,Css,Inline,Domdocument,我正在构建一个系统,要求不允许链接到CSS。它们允许将所有CSS内容放置在样式元素中。 我正在使用DOMDocument构建XML/XHTML CSS样式表大约有320行,因此我建议在单独的CSS文件中构建它们,并解决在DomDocument构建中插入CSS内容的问题 问题: 插入外部CSS文件内容的最佳方式是什么 并在DOMDocument内置样式元素之间放置它 Index.php <?php $xml = new DomDocument('1.0', 'UTF-8'); $xml-&

我正在构建一个系统,要求不允许链接到CSS。它们允许将所有CSS内容放置在样式元素中。 我正在使用DOMDocument构建XML/XHTML

CSS样式表大约有320行,因此我建议在单独的CSS文件中构建它们,并解决在DomDocument构建中插入CSS内容的问题

问题: 插入外部CSS文件内容的最佳方式是什么 并在DOMDocument内置样式元素之间放置它

Index.php

<?php

$xml = new DomDocument('1.0', 'UTF-8');
$xml->formatOutput = true;

$html = $xml->createElement('html');
$xml->appendChild($html);

$head = $xml->createElement('head');
$html->appendChild($head);
//
$style = $xml->createElement(
  'style',
  'css-content....' // The CSS content from external file should be inserted here.
);
$style->setAttribute('type', 'text/css');
$head->appendChild($style);

echo $xml->saveXML();
想要的结果

<?xml version="1.0" encoding="UTF-8"?>
<html>
  <head>

    <style type="text/css"> 

    body {
      background-color: pink;
    }

    </style>

  </head>
</html>

身体{
背景颜色:粉红色;
}

尝试以下方法:

并将
$style
更改为:

$style = $xml->createElement('style', $css);
它应该会起作用

$css = file_get_contents('main.css');
$style = $xml->createElement('style', $css);