Take part of the SVG ID content with PHP

Asked

Viewed 60 times

0

I have a file SVG and in it has several paths tagged and within the tag has the attribute id which in turn has the name of the state and also the municipality (which is what I need to take). I would like to take this municipality with the PHP, making a script to read the file and take inside each ID attribute only the municipality. The SVG content is like this:

<g id="x5F_Bahia_x5F_Nova_Viçosa">
    <path fill-rule="evenodd" clip-rule="evenodd" fill="#B3B3B3" stroke="#FFFFFF" stroke-width="0.2835" stroke-miterlimit="10" d="
        M430.19,291.31l-0.01,0.21l-0.67,0.36l-0.42-0.06l0.17-0.4l0.33-0.01l0.29,0.08L430.19,291.31L430.19,291.31z M424.84,290.44
        l0.68,0.34l0.33-0.17l0.5,0.11l0.45,0.25l0.17-0.09l0.15,0.01l0.32,0.25l0.57,0.09l0.13,0.18l1.03-0.01l-0.15,0.27l0.04,0.31
        l-0.43,0.15l-0.44,0.26l-0.7,0.71l-0.19-0.15l-0.56-0.06l-0.4,0.12l-0.22-0.04l-0.41,0.17l-0.81-0.03l-0.38-0.11l-3.18,0.03
        L421,292.8l0.03-0.17l-0.39-0.05l-0.06-1.23l0.31,0.01l0.45-0.2l0.14-0.05l0.22,0.02l0.37,0.19l0.22-0.06l0.3,0.02l0.04,0.04
        l0.11,0.03l0.17-0.07l0.24-0.22l0.11-0.02l0.25,0.05l0.34-0.15l0.23-0.07l0.03-0.11l0.45-0.26l0.2,0L424.84,290.44L424.84,290.44z"
        />
    <path fill="#B3B3B3" stroke="#FFFFFF" stroke-width="0.2835" stroke-miterlimit="8" d="M430.19,291.31l-0.01,0.21l-0.67,0.36
        l-0.42-0.06l0.17-0.4l0.33-0.01l0.29,0.08L430.19,291.31L430.19,291.31z M424.84,290.44l0.68,0.34l0.33-0.17l0.5,0.11l0.45,0.25
        l0.17-0.09l0.15,0.01l0.32,0.25l0.57,0.09l0.13,0.18l1.03-0.01l-0.15,0.27l0.04,0.31l-0.43,0.15l-0.44,0.26l-0.7,0.71l-0.19-0.15
        l-0.56-0.06l-0.4,0.12l-0.22-0.04l-0.41,0.17l-0.81-0.03l-0.38-0.11l-3.18,0.03L421,292.8l0.03-0.17l-0.39-0.05l-0.06-1.23
        l0.31,0.01l0.45-0.2l0.14-0.05l0.22,0.02l0.37,0.19l0.22-0.06l0.3,0.02l0.04,0.04l0.11,0.03l0.17-0.07l0.24-0.22l0.11-0.02
        l0.25,0.05l0.34-0.15l0.23-0.07l0.03-0.11l0.45-0.26l0.2,0L424.84,290.44L424.84,290.44z"/>
</g>
<g id="x5F_Bahia_x5F_Novo_Horizonte">

As you can see it has x5F_Bahia_x5F_Nova_Vizosa and x5F_Bahia_x5F_Novo_Horizonte. What I need to take is Nova_viçosa and New horizon. How can I do this using PHP ?

1 answer

2


If it’s always the same pattern (if id always has 'x5F' for example) even use regex, can with php do:

$id = 'x5F_Bahia_x5F_Nova_Viçosa';
$exp = explode('_x5F_', $id);
$zona = end($exp);
echo $zona; // Nova_Viçosa

With regex it can be like this:

$id = 'x5F_Bahia_x5F_Novo_Horizonte';
$hey = preg_match('/_x5F_(.*)$/', $id, $matches);
echo $matches[1]; // Novo_Horizonte

Browser other questions tagged

You are not signed in. Login or sign up in order to post.