PHP SVG 文件上的 Preg 替换导致分段错误
PHP Preg Replace on SVG file causes Segmentation Fault
当我在我的 SVG 文件上尝试 运行 这个命令时,浏览器说没有收到任何数据,我的 Apache 日志文件中出现错误。
preg_match("/(<g(\s|\S)*?<\/g>)/i", $SVG, $Matches);
我的 SVG 文件是 here。
我得到的实际错误是这个
[core:notice] [pid 20852] AH00052: child pid 31338 exit signal
Segmentation fault (11)
我做错了什么,我该如何解决?
使用PHP DOM functions to parse XML files. There is a good reason, why you shouldn't use RegEX to parse XML :-)
由于您要查找所有 <g ...></g>
标签,您可以这样做:
$xdoc = new DOMDocument;
// load your .svg
$xdoc->Load('0057b8.svg');
// get all "g"-tags
$gTags = $xdoc->getElementsByTagName('g');
// since the return is a DOMNodeList, we loop through all (even if it's only 1)
foreach($gTags as $gTag) {
// and here we can e.g. get the attributes
echo $gTag->getAttribute('transform') . PHP_EOL;
echo $gTag->getAttribute('fill') . PHP_EOL;
echo $gTag->getAttribute('stroke') . PHP_EOL;
// or set a new attribute
$gTag->setAttribute('stroke-width', '5');
}
g 标签将是 DOMElements,因此您可以阅读参考以获取所有可能的方法。
当我在我的 SVG 文件上尝试 运行 这个命令时,浏览器说没有收到任何数据,我的 Apache 日志文件中出现错误。
preg_match("/(<g(\s|\S)*?<\/g>)/i", $SVG, $Matches);
我的 SVG 文件是 here。
我得到的实际错误是这个
[core:notice] [pid 20852] AH00052: child pid 31338 exit signal Segmentation fault (11)
我做错了什么,我该如何解决?
使用PHP DOM functions to parse XML files. There is a good reason, why you shouldn't use RegEX to parse XML :-)
由于您要查找所有 <g ...></g>
标签,您可以这样做:
$xdoc = new DOMDocument;
// load your .svg
$xdoc->Load('0057b8.svg');
// get all "g"-tags
$gTags = $xdoc->getElementsByTagName('g');
// since the return is a DOMNodeList, we loop through all (even if it's only 1)
foreach($gTags as $gTag) {
// and here we can e.g. get the attributes
echo $gTag->getAttribute('transform') . PHP_EOL;
echo $gTag->getAttribute('fill') . PHP_EOL;
echo $gTag->getAttribute('stroke') . PHP_EOL;
// or set a new attribute
$gTag->setAttribute('stroke-width', '5');
}
g 标签将是 DOMElements,因此您可以阅读参考以获取所有可能的方法。