如何使用PHP获取SVG标签内容

如何使用PHP获取SVG标签内容

本文介绍了如何使用PHP获取SVG标签内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用PHP获取SVG标签内容.

I would like to get SVG tag content with PHP.

test.svg:

<?xml version="1.0" encoding="utf-8"?>
<!-- comment  -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
 width="202.5px" height="226.084px" viewBox="0 0 202.5 226.084" enable-background="new 0 0 202.5 226.084" xml:space="preserve">
   <g>
        <path d="M0,13.628c47.7940,13.628z"/>
        <polygon points="108.48,207.874 145.506,196.948 145.506,204.536 108.48,214.854  "/>
        <path fill="none" stroke="#000000" d="M114.55,223.959"/>
   </g>
   <g>
        <path fill="none" stroke="#000000" d="M114.55,223.959"/>
   </g>
   <anythingElse>
        <path fill="none" stroke="#000000" d="M114.55,223.959"/>
   <anythingElse>
</svg>

php:

$svg = new SimpleXMLElement( file_get_contents( 'test.svg' )  );

$svg->registerXPathNamespace('svg', 'http://www.w3.org/2000/svg');
$svg->registerXPathNamespace('xlink', 'http://www.w3.org/1999/xlink');

现在,我想以字符串的形式获取svg标签的内容.

Now, I would like to get the svg tag content as a string.

所需结果(硬编码示例):

Desired result (hardcoded example):

$content =  '<g>
    <path d="M0,13.628c47.7940,13.628z"/>
    <polygon points="108.48,207.874 145.506,196.948 145.506,204.536 108.48,214.854  "/>
    <path fill="none" stroke="#000000" d="M114.55,223.959"/>
</g>
   <g>
        <path fill="none" stroke="#000000" d="M114.55,223.959"/>
   </g>
   <anythingElse>
        <path fill="none" stroke="#000000" d="M114.55,223.959"/>
   <anythingElse> ';

//

我不想查找g标签:"/svg:svg/svg:g".由于不能保证svg内会有g标签.可能会有一个以上的g标签或其他一些标签.

I don't want to look for g tag: '/svg:svg/svg:g'.As there is no guarantee that inside the svg there will be a g tag. There could be more then one g tag or some other tags.

我想获得svg标记之间的所有内容.

I want to get everything between the opening and closing svg tags.

推荐答案

您已经看对了(更有可能:从),您需要在此处为​​xpath注册XML名称空间,因为XML中没有前缀:

You have already seen right (more likely: copy and pasted from an example code that has been given to you in a previous answer w/o understanding it further) that you need to register the XML namespace here for your xpath because there is no prefix for it in the XML:

<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
 width="202.5px" height="226.084px" viewBox="0 0 202.5 226.084" enable-background="new 0 0 202.5 226.084" xml:space="preserve">

它只是URI http://www.w3.org/2000/svg,没有给出前缀.它是该<svg>根元素的 the 命名空间.

It is just the URI http://www.w3.org/2000/svg, no prefix given. It is the namespace of that <svg> root element.

因此,您需要注册前缀(另一个已经加前缀的名称空间应该已经自动注册):

Therefore you need to register the prefix (the other already prefixed namespace should be already registered automatically):

$svg->registerXPathNamespace('svg', 'http://www.w3.org/2000/svg');

到目前为止,您要做什么?

Which is what you do so far.

现在要在xpath查询中使用您注册的前缀,请在标记名前添加前缀:

To now use your registered prefix in the xpath query, prefix the tagname with it:

/svg:svg/svg:g[1]

应为您提供该SVG名称空间中的第一个<g>元素.让我知道是否仍然需要进一步的解释/澄清.

Should give you the first <g> element in that SVG-Namespace. Let me know if this still needs further explanation / clarification.

请参见在线演示.

这也正是您上一个问题的原因概述:

This is also exactly as the reason outline to your previous question:

这篇关于如何使用PHP获取SVG标签内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:00