xSTVNx
Mitglied
ACHTUNG, bitte diesen Post überspringen
![Stick Out Tongue :P :P](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/1f61b.png)
Meine Furcht vor RegEx ist zwar überwunden, aber so richtig komme ich damit leider noch nicht klar.
![Confused :confused: :confused:](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/1f615.png)
Ich möchte bevor eine SVG-Datei in PNG umgewandelt wird, die
fill
-Attribute manipulieren bzw. ein <path>
-Element ganz löschen. Ersteres funktioniert (vorrausgesetzt die SVG-Datei folgt meinem Schema), aber das Löschen das Pfades gelingt mir nicht, da mir mal wohl mal wieder nicht klar ist, wie ich Zeichen wie bspw. /
oder "
korrekt via RegEx matchen lasse.Also neben
a-z
, A-Z
und 0+9
müssten auch .
/
=
und "
bzw. '
getroffen werden.
PHP:
$svg = file_get_contents('file.svg');
$newcols = ['FF0000','none','880088'];
$id = 1;
foreach( $newcols as $nc ){
if( $nc == 'none' ){ // delete path
$svg = preg_replace( '#<path id="a'.$id.'" ([0-9a-fA-F.="-/]+)>#' , '', $svg );
} else { // change fill
$svg = preg_replace( '/id="a'.$id.'" fill="#([0-9a-fA-F]{6})/' , 'id="a'.$id.'" fill="#'.$nc , $svg );
}
$id++;
}
Schema meiner
file.svg
XML:
<?xml version="1.0" encoding="UTF-8"?>
<svg ... >
<path id="a3" fill="#d40000" d="..." stroke-width="..." ... />
<path id="a2" fill="#000000" d="..." stroke-width="..." ... />
<path id="a1" fill="#eae44b" d="..." stroke-width="..." ... />
</svg>
Zuletzt bearbeitet: