How to change the attribute value of svg file

一曲冷凌霜 提交于 2019-12-05 10:46:41

Use DOMElement::setAttributeNS():

$xdoc = new DomDocument;
$xdoc->Load('svg.xml');
$tagName = $xdoc->getElementsByTagName('image')->item(0);
$attribNode = $tagName->getAttributeNode('xlink:href');

echo "Attribute Name  : " . $attribNode->name . "<br/>";
echo "Attribute Value : " . $attribNode->value;

$tagName->setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', 'image2.png');

echo $xdoc->saveXML();

One way could be to load the file as a string and then do the search and replace on it. You then can use loadXML http://www.php.net/manual/en/domdocument.loadxml.php and supply the changed string as a parameter.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!