我正在尝试从HTML中获取<svg>
内容并将其放入数组中供以后使用。我的代码......
$html_content = '
<div class="stage" id="shape_1"><svg height="100" version="1.1" width="350" xmlns="http://www.w3.org/2000/svg" style="overflow: hidden; position: relative; left: -0.316681px; top: -0.650024px;"><desc>Created with Raphaël 2.1.2</desc><defs/><rect x="75" y="25" width="200" height="50" r="0" rx="0" ry="0" fill="#90ee90" stroke="#000" style="fill-opacity: 0.5;" fill-opacity="0.5" stroke-width="0"/><path style="stroke-opacity: 0.8;" fill="none" stroke="#666666" d="M75,25L275,25" stroke-width="2" stroke-opacity="0.8"/><path style="stroke-opacity: 0.8;" fill="none" stroke="#666666" d="M275,25L275,75" stroke-width="2" stroke-opacity="0.8"/><path style="stroke-opacity: 0.8;" fill="none" stroke="#666666" d="M275,75L75,75" stroke-width="2" stroke-opacity="0.8"/><path style="stroke-opacity: 0.8;" fill="none" stroke="#666666" d="M75,75L75,25" stroke-width="2" stroke-opacity="0.8"/><text style="text-anchor: middle; font: 15px Arial;" x="175" y="85" text-anchor="middle" font="10px "Arial"" stroke="none" fill="#000000" transform="matrix(1,0,0,1,0,6.5)" font-family="Arial" font-size="15px" font-style="normal" font-weight="normal"><tspan dy="5">x + 10 ft.</tspan></text><text style="text-anchor: end; font: 15px Arial;" x="65" y="50" text-anchor="middle" font="10px "Arial"" stroke="none" fill="#000000" font-family="Arial" font-size="15px" font-style="normal" font-weight="normal"><tspan dy="5">x ft.</tspan></text></svg></div>';
$doc = new DOMDocument();
$doc->loadHTML($html_content);
$svg = $doc->getElementsByTagName('svg');
foreach ($svg as $svg_item) {
array_push($svg_array, $svg_item);
}
print_r($svg_array);
问题是$svg_array
为空。我知道<svg>
中肯定有$html_content
个内容。
“警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:意外的结束标记:实体中的div,行中的:46 ...”
或
“警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:标签c在实体中无效,行:119在......”
修改:经过深入调查,我的代码的精简版会产生以下警告......
警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:标签svg在实体中无效,行:2在.....中
警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:标签desc在实体中无效,行:2在......
警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:标签defs在实体中无效,行:2在......
警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:标签在实体中无效,行:2在......
警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:标记路径在实体中无效,行:2在.....中
警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:标记路径在实体中无效,行:2在.....中
警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:标记路径在实体中无效,行:2在.....中
警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:标记路径在实体中无效,行:2在.....中
警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:标记文本在实体中无效,行:2在.....中
警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:标记tspan在实体中无效,行:2在.....中
警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:标记文本在实体中无效,行:2在.....中
警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:标记tspan在实体中无效,行:2在.....中
可能不支持<svg>
????
思考吗
答案 0 :(得分:0)
嗯,这可能是PHP不支持这个吗?
以下示例通过javascripts getElementsByTagName获取svg:
var e = document.getElementsByTagName('svg');
console.log(e[0]);
&#13;
<svg viewBox="0 0 100 100">
</svg>
&#13;
答案 1 :(得分:0)
此功能不适用于SVG等HTML5元素。 Web上的大多数建议都是关闭错误,以使其与HTML5一起使用。
答案 2 :(得分:0)
现在可以更容易地将问题分开,因为包含了违规的html字符串,并且可以通过将输入文本分布在多行上来进一步研究。完成后,我们得到一个超过17行的输入字符串。然后,我们可以使用警告消息和行号来快速识别损坏解析器的输入部分。
'<div class="stage" id="shape_1">
<svg height="100" version="1.1" width="350" xmlns="http://www.w3.org/2000/svg" style="overflow: hidden; position: relative; left: -0.316681px; top: -0.650024px;">
<desc>Created with Raphaël 2.1.2</desc>
<defs/>
<rect x="75" y="25" width="200" height="50" r="0" rx="0" ry="0" fill="#90ee90" stroke="#000" style="fill-opacity: 0.5;" fill-opacity="0.5" stroke-width="0"/>
<path style="stroke-opacity: 0.8;" fill="none" stroke="#666666" d="M75,25L275,25" stroke-width="2" stroke-opacity="0.8"/>
<path style="stroke-opacity: 0.8;" fill="none" stroke="#666666" d="M275,25L275,75" stroke-width="2" stroke-opacity="0.8"/>
<path style="stroke-opacity: 0.8;" fill="none" stroke="#666666" d="M275,75L75,75" stroke-width="2" stroke-opacity="0.8"/>
<path style="stroke-opacity: 0.8;" fill="none" stroke="#666666" d="M75,75L75,25" stroke-width="2" stroke-opacity="0.8"/>
<text style="text-anchor: middle; font: 15px Arial;" x="175" y="85" text-anchor="middle" font="10px "Arial"" stroke="none" fill="#000000" transform="matrix(1,0,0,1,0,6.5)" font-family="Arial" font-size="15px" font-style="normal" font-weight="normal">
<tspan dy="5">x + 10 ft.</tspan>
</text>
<text style="text-anchor: end; font: 15px Arial;" x="65" y="50" text-anchor="middle" font="10px "Arial"" stroke="none" fill="#000000" font-family="Arial" font-size="15px" font-style="normal" font-weight="normal">
<tspan dy="5">x ft.</tspan>
</text>
</svg>
</div>';
现在,如果我们停下来思考一下,SVG实际上并不是HTML - 它也是XML的一种方言。同样的父级,但仍然不是同一个,当我们以您拥有的方式使用SVG时,我们在XML中嵌入XML是因为缺乏更好的术语。考虑到这一点,17条输入线导致以下警告信息几乎不足为奇。提到的实体确实不是标准的HTML实体。 (标签是每行的第5个字 - svg,desc,defs等等)
Warning: DOMDocument::loadHTML(): Tag svg invalid in Entity, line: 2 in C:\xampp2\htdocs\*redacted*\svg.php on line 23
Warning: DOMDocument::loadHTML(): Tag desc invalid in Entity, line: 3 in C:\xampp2\htdocs\*redacted*\svg.php on line 23
Warning: DOMDocument::loadHTML(): Tag defs invalid in Entity, line: 4 in C:\xampp2\htdocs\*redacted*\svg.php on line 23
Warning: DOMDocument::loadHTML(): Tag rect invalid in Entity, line: 5 in C:\xampp2\htdocs\*redacted*\svg.php on line 23
Warning: DOMDocument::loadHTML(): Tag path invalid in Entity, line: 6 in C:\xampp2\htdocs\*redacted*\svg.php on line 23
Warning: DOMDocument::loadHTML(): Tag path invalid in Entity, line: 7 in C:\xampp2\htdocs\*redacted*\svg.php on line 23
Warning: DOMDocument::loadHTML(): Tag path invalid in Entity, line: 8 in C:\xampp2\htdocs\*redacted*\svg.php on line 23
Warning: DOMDocument::loadHTML(): Tag path invalid in Entity, line: 9 in C:\xampp2\htdocs\*redacted*\svg.php on line 23
Warning: DOMDocument::loadHTML(): Tag text invalid in Entity, line: 10 in C:\xampp2\htdocs\*redacted*\svg.php on line 23
Warning: DOMDocument::loadHTML(): Tag tspan invalid in Entity, line: 11 in C:\xampp2\htdocs\*redacted*\svg.php on line 23
Warning: DOMDocument::loadHTML(): Tag text invalid in Entity, line: 13 in C:\xampp2\htdocs\*redacted*\svg.php on line 23
Warning: DOMDocument::loadHTML(): Tag tspan invalid in Entity, line: 14 in C:\xampp2\htdocs\*redacted*\svg.php on line 23
那么,该怎么办?简单。而不是尝试使用loadHTML方法加载XML,而只需使用loadXML方法。完成后,输出变为:
Array ( [0] => DOMElement Object ( [tagName] => svg [schemaTypeInfo] => [nodeName] => svg [nodeValue] => Created with Raphaël 2.1.2 x + 10 ft. x ft. [nodeType] => 1 [parentNode] => (object value omitted) [childNodes] => (object value omitted) [firstChild] => (object value omitted) [lastChild] => (object value omitted) [previousSibling] => (object value omitted) [nextSibling] => (object value omitted) [attributes] => (object value omitted) [ownerDocument] => (object value omitted) [namespaceURI] => http://www.w3.org/2000/svg [prefix] => [localName] => svg [baseURI] => file:/C:/xampp2/htdocs/*redacted*/ [textContent] => Created with Raphaël 2.1.2 x + 10 ft. x ft. ) )