0

我有一个包含坐标的文件,并且想将这些坐标提取到变量x1, x2, 中y1y2这样我就可以使用 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/>";

  }
}
}
4

3 回答 3

0

here's a quick and dirty solution which works:

<?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);
?>

This leads to this output:

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] => )

Now you can access the needed elements of the array with: $points["x1"] $points["y1"] ...

Hope this helps

To answer the question in your comment, here is the solution:

$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);

leads to:

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 )
于 2013-02-04T21:57:35.770 回答
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,
  ),
)
于 2013-02-04T22:01:48.227 回答
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.08295576487825)56,

$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)
  }
}
于 2013-02-04T22:02:39.560 回答