【发布时间】:2019-01-30 12:50:52
【问题描述】:
我正在尝试在现有 svg 中添加一个 def 元素,但插入的节点数据是一个字符串,而不是像这样的 css 标记(包含“<\;”,整个节点值用双引号括起来):
"<defs xmlns="http://www.w3.org/2000/svg"><style type="text/css">\;@font-face {font-family: DaimlerCS-Demi;src: url('../fonts/CorporateS/CorporateS-Demi.otf');}<\;/style></defs>"
我们如何正确地为 svg 中的 defs 添加一个 css 节点?这是我现有的 php 代码:
$data = trim(file_get_contents('php://input'));
$svg = new SimpleXMLElement($data);
$svg->registerXPathNamespace('svg', 'http://www.w3.org/2000/svg');
$defContent = <<<EOF
<style type="text/css">@font-face {font-family: DaimlerCS-Demi;src:
url('../fonts/CorporateS/CorporateS-Demi.otf');}</style>
EOF;
$def = $svg->addChild("defs", $defContent);
$svg = $svg->asXML();
$filePath = "./res/" . base64_encode(time()) . ".svg";
file_put_contents($filePath, $svg);
另外,如果可能的话,我们如何将节点添加为第一个子节点,而不是作为最后一个子节点插入
【问题讨论】: