我有一个文件,其中包含坐标,并希望将这些坐标提取到变量x1
,x2
,y1
,y2
中,以便我可以获得中心线使用y = y2-y1和x = x2-x1
例如,我想像这样转换文件数据:
points="94.08955764770508,275.3258819580078 99.92155838012695,275.3258819580078 99.92155838012695,281.16587829589844 94.08955764770508,281.16587829589844"
进入这样的变量:
x1 = 94.08955764770508
y1 = 275.3258819580078
x2 = 99.92155838012695
y2 = 275.3258819580078
以下是我一直在尝试的代码:
$line = '<polygon id="svg_806" fill-rule="nonzero" fill="black" stroke-linejoin="round" stroke-width="0" points="94.08955764770508,275.3258819580078 99.92155838012695,275.3258819580078 99.92155838012695,281.16587829589844 94.08955764770508,281.16587829589844 "/>';
if (strpos($line,'<polygon') !== false) {
$a = 1;
for ($i=0; $i <= 6; $i++) {
$cordinates = explode('points="', $line);
$cordinates = substr($cordinates[$i], 0, strpos($cordinates[$i], '"'));
foreach(explode(' ', $cordinates) as $value) {
$parts = explode(",", $value);
echo trim($parts[0])."<br/>".trim($parts[1])."<br/>";
}
}
}
答案 0 :(得分:0)
这是一个快速而肮脏的解决方案:
<?php
$line = '<polygon id="svg_806" fill-rule="nonzero" fill="black" stroke-linejoin="round" stroke-width="0" points="94.08955764770508,275.3258819580078 99.92155838012695,275.3258819580078 99.92155838012695,281.16587829589844 94.08955764770508,281.16587829589844 "/>';
$xml = simplexml_load_string($line);
$ps = explode(" ", $xml->attributes()->points);
$points = array();
$i = 1;
foreach($ps as $point){
$exploded = explode(",", $point);
$points["x".$i] = $exploded[0];
$points["y".$i] = $exploded[1];
$i++;
}
print_r($points);
?>
这导致了这个输出:
Array ( [x1] => 94.08955764770508 [y1] => 275.3258819580078 [x2] => 99.92155838012695 [y2] => 275.3258819580078 [x3] => 99.92155838012695 [y3] => 281.16587829589844 [x4] => 94.08955764770508 [y4] => 281.16587829589844 [x5] => [y5] => )
现在您可以使用以下命令访问数组所需的元素: $ points [“x1”] $ points [“y1”] ...
希望这有帮助
要回答评论中的问题,以下是解决方案:
$input = "2957 1620 1124 3836 1524 3836 1524 3684 1924 3684 3324 3838 3724 3838 584 3574";
$i = $count = 1;
$points = array();
$exploded = explode(" ", $input);
foreach ($exploded as $current){
if($count % 2 == 1){
//X
$points["x".$i] = $current;
}else{
//y
$points["y".$i] = $current;
$i++;
}
$count++;
}
print_r($points);
导致:
Array ( [x1] => 2957 [y1] => 1620 [x2] => 1124 [y2] => 3836 [x3] => 1524 [y3] => 3836 [x4] => 1524 [y4] => 3684 [x5] => 1924 [y5] => 3684 [x6] => 3324 [y6] => 3838 [x7] => 3724 [y7] => 3838 [x8] => 584 [y8] => 3574 )
答案 1 :(得分:0)
<强>代码:强>
// Your SVG
$line = '<polygon id="svg_806" fill-rule="nonzero" fill="black" stroke-linejoin="round" stroke-width="0" points="94.08955764770508,275.3258819580078 99.92155838012695,275.3258819580078 99.92155838012695,281.16587829589844 94.08955764770508,281.16587829589844 "/>';
// Parse SVG as XML and extract 'points' attribute
$xml = new SimpleXMLElement($line);
$points = (string) $xml->attributes()->points;
// Parse points attribute
preg_match_all('/([^, ]+),([^, ]+)/', $points, $matches, PREG_SET_ORDER);
// Convert matches from regular expression to nice array
$point_coords = array_map(function($m) {
return array('x' => (float) $m[1], 'y' => (float) $m[2]);
}, $matches);
// Now you have array of points array('x' => float, 'y' => float)
echo var_export($point_coords);
<强>输出:强>
array (
0 =>
array (
'x' => 94.089557647705,
'y' => 275.32588195801,
),
1 =>
array (
'x' => 99.921558380127,
'y' => 275.32588195801,
),
2 =>
array (
'x' => 99.921558380127,
'y' => 281.1658782959,
),
3 =>
array (
'x' => 94.089557647705,
'y' => 281.1658782959,
),
)
答案 2 :(得分:0)
我认为此代码可以为您提供帮助:
<?php
// Your XML string
$xmlStr = '<polygon id="svg_806" fill-rule="nonzero" fill="black" stroke-linejoin="round" stroke-width="0" points="94.08955764770508,275.3258819580078 99.92155838012695,275.3258819580078 99.92155838012695,281.16587829589844 94.08955764770508,281.16587829589844 "/>';
// Read the XML into and PHP object, making it ease to access structerd data
$xml = new SimpleXMLElement($xmlStr);
// Extracts the string inside "points" parameter in polygon (main) tag
$pointsStr = (string)$xml->attributes()->points;
// Breaks the string in pieces of space separated points, like "94.08955764770508,275.3258819580078"
$pointsArr = explode(' ', trim($pointsStr));
// The result array, each element is a point (an array with 2 positions)
$points = array();
foreach ($pointsArr as $pointStr) {
// Reads the float data for each position using C-like sscanf
list($x, $y) = sscanf($pointStr, '%f,%f');
// Echo and save
echo "Read point ($x, $y)<br>\n";
$points[] = array($x, $y);
}
?>
它回应:
读取点(94.089557647705,275.32588195801)
读取点(99.921558380127,275.32588195801)
读点(99.921558380127,281.1658782959)
读点(94.089557647705,281.1658782959)
$points
将是:
array(4) {
[0]=>
array(2) {
[0]=>
float(94.089557647705)
[1]=>
float(275.32588195801)
}
[1]=>
array(2) {
[0]=>
float(99.921558380127)
[1]=>
float(275.32588195801)
}
[2]=>
array(2) {
[0]=>
float(99.921558380127)
[1]=>
float(281.1658782959)
}
[3]=>
array(2) {
[0]=>
float(94.089557647705)
[1]=>
float(281.1658782959)
}
}