DomDocument 在样式元素中从文件中插入 CSS
DomDocument inserting CSS from file in style element
我正在构建一个系统,要求不允许链接到 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();
Main.css
body {
background-color: pink;
}
想要的结果
<?xml version="1.0" encoding="UTF-8"?>
<html>
<head>
<style type="text/css">
body {
background-color: pink;
}
</style>
</head>
</html>
沿着这些方向尝试一些事情:
添加
$css = file_get_contents('main.css');
并将 $style
更改为:
$style = $xml->createElement('style', $css);
它应该可以工作。
我正在构建一个系统,要求不允许链接到 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();
Main.css
body {
background-color: pink;
}
想要的结果
<?xml version="1.0" encoding="UTF-8"?>
<html>
<head>
<style type="text/css">
body {
background-color: pink;
}
</style>
</head>
</html>
沿着这些方向尝试一些事情:
添加
$css = file_get_contents('main.css');
并将 $style
更改为:
$style = $xml->createElement('style', $css);
它应该可以工作。