我想读取一个 SVG 文件并从该文件中获取 SVG 标签(因为我想在 html 中显示 svg,例如 <div><svg>...</svg></div>
而没有 xml header )。
并在浏览器中像 HTML 一样显示这个 svg 标签——像 SVG 图像一样打印这个 SVG 标签。因为现在我得到了错误的输出“DOMNodeList Object ( [length] => 1 )”。
PHP
$doc = new DOMDocument();
$doc->load('http://example.com/logo.svg');
$svg = $doc->getElementsByTagName('svg');
echo "<div style='width: 100%, height: 100%; '>";
print_r($svg); // DOMNodeList Object ( [length] => 1 )
echo "</div>";
最佳答案
我找到了解决方案,但这并不是我的问题的确切答案。所以我不会将其标记为答案,但我将此解决方案留在这里。也许会有人需要它...:)
我只是读取文件内容,然后寻找字符串"
PHP
<?php
$svg_file = file_get_contents('http://example.com/logo.svg');
$find_string = '<svg';
$position = strpos($svg_file, $find_string);
$svg_file_new = substr($svg_file, $position);
echo "<div style='width:100%; height:100%;' >" . $svg_file_new . "</div>";
?>
关于PHP 从 SVG 文件中获取 svg 标签,并在 DIV 中以 HTML 显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29991284/