Bilal Bilal - 27 days ago 22
PHP Question

change svg fill attribute value using php

I have been trying to change

fill
attribute of text tag in my svg image.
But it's not working.

text tag

<text style="font-variant:normal;font-weight:bold;font-size:9.99989986px;font-family:'Times New Roman';-inkscape-font-specification:TimesNewRomanPSMT-Bold;writing-mode:lr-tb;fill-opacity:1;fill-rule:nonzero;stroke:none"
transform="matrix(0,1,1,0,27.7761,21.4725)"
fill="#231f20"
name="mytitle"
id="mytitle"
data-svg="text">
<tspan
id="tspan7549"
sodipodi:role="line"
y="0"
x="0">TEST TEXT</tspan>
</text>


php code

$dom->loadXML($svgString);
foreach($dom->getElementById('mytitle') as $mytitle) {
$mytitle->setAttribute('http://www.w3.org/2000/svg','fill','#ffffff');

}
$dom->saveXML()


I have no idea why this is not working. i have written a code for image replacement(
$dom->getElementsByTagName('image')
) and it's workin perfectly.

I want to change my fill attribute value using php.Any kind of help would be appreciated.

Answer

setAttribute() only takes two parameters. The three parameter version is setAttributeNS().

http://php.net/manual/en/domelement.setattribute.php http://php.net/manual/en/domelement.setattributens.php

However when setting attributes in SVG elements, you don't need to use the namespace. The following should work:

$mytitle->setAttribute('fill','#ffffff');