I have an SVG:
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 17.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!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="Filled_Icons" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px"
y="0px" width="24px" height="24px" viewBox="0 0 24 24" enable-background="new 0 0 24 24" xml:space="preserve">
<path d="M21.499,2.506c-0.827,0-1.5,0.671-1.5,1.5c0,0.827-0.673,1.5-1.5,1.5c-0.827,0-1.5-0.673-1.5-1.5c0-1.653-1.346-3-2.999-3
c-1.654,0-3,1.347-3,3v2H9.5c-0.276,0-0.5,0.223-0.5,0.5v1.09c-3.005,1.217-5,4.15-5,7.41c0,4.411,3.589,8,8,8s7.999-3.589,7.999-8
c0-3.26-1.994-6.193-4.999-7.41v-1.09c0-0.277-0.224-0.5-0.5-0.5H12v-2c0-1.104,0.896-2,2-2c1.102,0,1.999,0.896,1.999,2
c0,1.378,1.122,2.5,2.5,2.5c1.379,0,2.5-1.122,2.5-2.5c0-0.275,0.225-0.5,0.5-0.5c0.276,0,0.5-0.225,0.5-0.5
C21.999,2.73,21.775,2.506,21.499,2.506z M9.141,10.904c-2.262,1.577-2.819,4.699-1.242,6.962C8.056,18.092,8,18.402,7.773,18.561
c-0.087,0.06-0.186,0.089-0.285,0.089c-0.158,0-0.313-0.073-0.41-0.212c-0.917-1.316-1.267-2.908-0.984-4.486
C6.375,12.373,7.255,11,8.569,10.084c0.227-0.158,0.538-0.103,0.696,0.124C9.423,10.434,9.367,10.747,9.141,10.904z"/>
</svg>
I'm trying to extract only the <svg>..</svg>
tag using:
$xml = simplexml_load_file( $file );
// (string) $xml->svg
echo print_r($xml,1);
.. but the returned object looks quite different:
SimpleXMLElement Object
(
[@attributes] => Array
(
[version] => 1.1
[id] => Filled_Icons
[x] => 0px
[y] => 0px
[width] => 24px
[height] => 24px
[viewBox] => 0 0 24 24
[enable-background] => new 0 0 24 24
)
[g] => SimpleXMLElement Object
(
[path] => SimpleXMLElement Object
(
[@attributes] => Array
(
[d] => M9.188,2.609c0.255,0.104,0.378,0.396,0.274,0.652C9.358,3.516,9.065,3.639,8.811,3.535 C7.571,3.031,6.031,3.083,4.979,3.665C4.679,3.833,4.322,4.102,4.132,4.496C5.438,5.14,5.905,6.313,5.905,6.958 c0,0.277-0.224,0.5-0.5,0.5c-0.275,0-0.5-0.223-0.5-0.5c0-0.018-0.152-1.763-2.592-1.907C1.812,5.042,1.226,5.292,0.778,5.733 C0.29,6.215,0.01,6.852,0.01,7.479c0,0.015-0.008,0.029-0.009,0.044C0.009,8.449,0.499,9.261,1.316,9.7 c0.647,0.346,1.399,0.389,2.064,0.138c0.374,0.593,0.997,1.051,1.83,1.341c1.334,0.463,3.067,0.419,4.18-0.068 c0.547,0.613,0.603,1.024,0.608,2.39c-0.405,0.001-0.812,0.1-1.183,0.299C7.992,14.242,7.499,15.064,7.499,16 c0,0.914,0.476,1.726,1.271,2.175c0.378,0.214,0.79,0.322,1.203,0.325c-0.225,2.024-1.87,2.381-4.289,2.67 c-1.728,0.207-3.686,0.441-3.686,2.329c0,0.275,0.225,0.5,0.5,0.5h19c0.276,0,0.5-0.225,0.5-0.5c0-1.888-1.957-2.122-3.685-2.329 c-2.419-0.29-4.064-0.646-4.289-2.67c0.413-0.003,0.825-0.111,1.203-0.325c0.796-0.449,1.271-1.262,1.271-2.175 c0-0.936-0.492-1.758-1.316-2.201C14.811,13.602,14.404,13.502,14,13.5c0.006-1.366,0.062-1.779,0.608-2.392 c0.955,0.41,2.43,0.516,3.649,0.229c0.885-0.209,1.594-0.596,2.078-1.132c0.685,0.367,1.498,0.396,2.224,0.057 C23.447,9.847,23.999,8.98,23.999,8c0-0.652-0.252-1.286-0.737-1.767c-0.417-0.406-0.997-0.691-1.558-0.682 c-2.417,0.143-2.569,1.889-2.569,1.906c0,0.277-0.224,0.5-0.5,0.5s-0.5-0.223-0.5-0.5c0-0.669,0.503-1.909,1.919-2.534 c-0.211-0.565-0.239-0.842-0.993-1.259c-1.052-0.582-2.591-0.635-3.831-0.129c-0.256,0.104-0.547-0.019-0.651-0.274 c-0.104-0.256,0.019-0.548,0.274-0.652c0.476-0.192,0.989-0.311,1.511-0.369c-1.118-1.092-2.683-1.74-4.364-1.74 c-1.679,0-3.241,0.646-4.358,1.736C8.174,2.292,8.701,2.411,9.188,2.609z
)
)
)
)
How to extract only the svg tag, so the expected output is:
<svg version="1.1" id="Filled_Icons" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px"
y="0px" width="24px" height="24px" viewBox="0 0 24 24" enable-background="new 0 0 24 24" xml:space="preserve">
<path d="M21.499,2.506c-0.827,0-1.5,0.671-1.5,1.5c0,0.827-0.673,1.5-1.5,1.5c-0.827,0-1.5-0.673-1.5-1.5c0-1.653-1.346-3-2.999-3
c-1.654,0-3,1.347-3,3v2H9.5c-0.276,0-0.5,0.223-0.5,0.5v1.09c-3.005,1.217-5,4.15-5,7.41c0,4.411,3.589,8,8,8s7.999-3.589,7.999-8
c0-3.26-1.994-6.193-4.999-7.41v-1.09c0-0.277-0.224-0.5-0.5-0.5H12v-2c0-1.104,0.896-2,2-2c1.102,0,1.999,0.896,1.999,2
c0,1.378,1.122,2.5,2.5,2.5c1.379,0,2.5-1.122,2.5-2.5c0-0.275,0.225-0.5,0.5-0.5c0.276,0,0.5-0.225,0.5-0.5
C21.999,2.73,21.775,2.506,21.499,2.506z M9.141,10.904c-2.262,1.577-2.819,4.699-1.242,6.962C8.056,18.092,8,18.402,7.773,18.561
c-0.087,0.06-0.186,0.089-0.285,0.089c-0.158,0-0.313-0.073-0.41-0.212c-0.917-1.316-1.267-2.908-0.984-4.486
C6.375,12.373,7.255,11,8.569,10.084c0.227-0.158,0.538-0.103,0.696,0.124C9.423,10.434,9.367,10.747,9.141,10.904z"/>
</svg>
Ofcourse I could just use regex (/(<svg.*?\/svg>)/s
) but I'm looking for an elegant approach.