0

座標を持つファイルが 1 つあり、これらの座標を変数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/>";

  }
}
}
4

3 に答える 3

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.0895576477016、58125)

$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 に答える
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 )
于 2013-02-04T21:57:35.770 に答える