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

时间:2022-06-01 20:02:18

I would like to get SVG tag content with PHP.

我想用PHP获取SVG标记内容。

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');

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

现在,我想将svg标记内容作为字符串。

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> ';

//EDIT:

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.

我不想寻找g标签:'/ svg:svg / svg:g'。由于无法保证svg内部会有g标签。可能有超过一个g标签或一些其他标签。

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

我想获得开始和结束svg标签之间的所有内容。

2 个解决方案

#1


4  

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:

您已经看到了正确的(更有可能的是:复制并粘贴在之前的答案中已经提供给你的示例代码,而不是进一步了解它),你需要在这里为你的xpath注册XML命名空间,因为没有前缀对于它在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">

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

它只是URI http://www.w3.org/2000/svg,没有给出前缀。它是 根元素的名称空间。

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.

你到目前为止做了什么。

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

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

/svg:svg/svg:g[1]

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

应该为您提供该SVG命名空间中的第一个 元素。如果还需要进一步解释/澄清,请告诉我。

See the online demo.

请参阅在线演示。

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

这也正是您之前提问的原因:

#2


1  

I think to get onle the g tag you don;t need namespace you could try this:

我想要获得你不喜欢的g标签;你需要命名空间你可以试试这个:

$result = $svg->xpath('svg/g');
foreach ($result as $gtag) {
    echo $gtag . "\n";
}

I can't test it though:)

我不能测试它:)

#1


4  

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:

您已经看到了正确的(更有可能的是:复制并粘贴在之前的答案中已经提供给你的示例代码,而不是进一步了解它),你需要在这里为你的xpath注册XML命名空间,因为没有前缀对于它在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">

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

它只是URI http://www.w3.org/2000/svg,没有给出前缀。它是 根元素的名称空间。

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.

你到目前为止做了什么。

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

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

/svg:svg/svg:g[1]

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

应该为您提供该SVG命名空间中的第一个 元素。如果还需要进一步解释/澄清,请告诉我。

See the online demo.

请参阅在线演示。

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

这也正是您之前提问的原因:

#2


1  

I think to get onle the g tag you don;t need namespace you could try this:

我想要获得你不喜欢的g标签;你需要命名空间你可以试试这个:

$result = $svg->xpath('svg/g');
foreach ($result as $gtag) {
    echo $gtag . "\n";
}

I can't test it though:)

我不能测试它:)