PHP从SVG文件中获取svg标记,并在DIV中以HTML格式显示 [英] PHP get svg tag from SVG file, and show it in HTML in DIV

查看:396
本文介绍了PHP从SVG文件中获取svg标记,并在DIV中以HTML格式显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取一个SVG文件并从该文件中获取SVG标签(因为我想在html中显示svg,例如<div><svg>...</svg></div>而不显示xml标头).

I want to read an SVG file and get the SVG tag from this file (because I want to show svg in html e.g. <div><svg>...</svg></div> without the xml header).

并在类似HTML的浏览器中显示此svg标记-像SVG图像一样打印此SVG标记.因为现在我弄错了输出" DOMNodeList对象([length] => 1)".

And show this svg tag in browser like HTML - print this SVG TAG like SVG image. Becouse now I'm gettong wrong output "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>";

推荐答案

我找到了解决方案,但这并不是我所提出问题的确切答案.因此,我不会将其标记为答案,但我将在此处保留此解决方案.也许会有人需要它...:)

I found solution, but it is not exactly the answer for my question. So I will not mark it as a answer, but I leave here this solution. Maybe there will be somebody who will need it... :)

我只是读取文件内容,然后寻找字符串< svg" 的位置,然后减去这段代码.

I just read file content, then I look for position of string "< svg" , and then substract this piece of code.

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格式显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆