2010-05-15 138 views
0

我得到一个“解析错误:语法错误,意外的'{'在第2行”。我没有看到问题。PHP解析错误意外'{'

<?php 
class pointLocation { 
    var $pointOnVertex = true; // Check if the point sits exactly on one of the vertices 

    function pointLocation() { 
    } 
     
     
        function pointInPolygon($point, $polygon, $pointOnVertex = true) { 
        $this->pointOnVertex = $pointOnVertex; 
         
        // Transform string coordinates into arrays with x and y values 
        $point = $this->pointStringToCoordinates($point); 
        $vertices = array();  
        foreach ($polygon as $vertex) { 
            $vertices[] = $this->pointStringToCoordinates($vertex);  
        } 
         
        // Check if the point sits exactly on a vertex 
        if ($this->pointOnVertex == true and $this->pointOnVertex($point, $vertices) == true) { 
            return "vertex"; 
        } 
         
        // Check if the point is inside the polygon or on the boundary 
        $intersections = 0;  
        $vertices_count = count($vertices); 
     
        for ($i=1; $i < $vertices_count; $i++) { 
            $vertex1 = $vertices[$i-1];  
            $vertex2 = $vertices[$i]; 
            if ($vertex1['y'] == $vertex2['y'] and $vertex1['y'] == $point['y'] and $point['x'] > min($vertex1['x'], $vertex2['x']) and $point['x'] < max($vertex1['x'], $vertex2['x'])) { // Check if point is on an horizontal polygon boundary 
                return "boundary"; 
            } 
            if ($point['y'] > min($vertex1['y'], $vertex2['y']) and $point['y'] <= max($vertex1['y'], $vertex2['y']) and $point['x'] <= max($vertex1['x'], $vertex2['x']) and $vertex1['y'] != $vertex2['y']) {  
                $xinters = ($point['y'] - $vertex1['y']) * ($vertex2['x'] - $vertex1['x']) / ($vertex2['y'] - $vertex1['y']) + $vertex1['x'];  
                if ($xinters == $point['x']) { // Check if point is on the polygon boundary (other than horizontal) 
                    return "boundary"; 
                } 
                if ($vertex1['x'] == $vertex2['x'] || $point['x'] <= $xinters) { 
                    $intersections++;  
                } 
            }  
        }  
        // If the number of edges we passed through is even, then it's in the polygon.  
        if ($intersections % 2 != 0) { 
            return "inside"; 
        } else { 
            return "outside"; 
        } 
    } 

     
     
    function pointOnVertex($point, $vertices) { 
        foreach($vertices as $vertex) { 
            if ($point == $vertex) { 
                return true; 
            } 
        } 
     
    } 
         
     
    function pointStringToCoordinates($pointString) { 
        $coordinates = explode(" ", $pointString); 
        return array("x" => $coordinates[0], "y" => $coordinates[1]); 
    } 
     
     
} 



$pointLocation = new pointLocation(); 
$points = array("30 19", "0 0", "10 0", "30 20", "11 0", "0 11", "0 10", "30 22", "20 20"); 
$polygon = array("10 0", "20 0", "30 10", "30 20", "20 30", "10 30", "0 20", "0 10", "10 0"); 
foreach($points as $key => $point) { 
    echo "$key ($point) is " . $pointLocation->pointInPolygon($point, $polygon) . "<br>"; 
} 
?> 

有没有人看到这个问题?

感谢,

-Laxmidi

+0

什么版本的PHP在你运行的平​​台上运行?看起来像月亮臭虫的着名的PHP阶段。 – 2010-05-15 17:32:43

回答

0

对我的工作很好,所以它听起来像一个编码或EOL问题。试试这些东西:

  • 确保您的文件是UNIX EOL格式。每行应以换行符结尾(LF)
  • 确保文件中没有BOM(字节顺序标记)。
  • 你可以尝试在你的文件末尾摆脱?>。它没有任何用处。当然,这并不能真正帮助你解决问题,但它会让你的代码更加清洁。
  • 尝试添加一个换行符<?php后和class pointLocation {后。有些版本的PHP在奇怪的空白处出现。
  • 确保您的文件是什么标准,如UTF-8

一个好编辑,以确保您的文件符合这些标准的Notepad++。有很多选择,他们使它很容易。不要使用普通的旧记事本,因为Microsoft会将您锁定为CRLF格式(不适合PHP),并且不允许您控制编码。

祝你好运!

+0

嗨mattbasta, 谢谢你的建议。这听起来像代码适用于其他人。我使用Dreamweaver编辑代码。如何检查UNIX EOL格式? -Laxmidi – Laxmidi 2010-05-15 17:49:27

3

进行了测试。无差错运行,并产生以下outout:

0 (30 19) is boundary<br>1 (0 0) is outside<br>2 (10 0) is vertex<br>3 (30 20) is vertex<br>4 (11 0) is boundary<br>5 (0 11) is boundary<br>6 (0 10) is vertex<br>7 (30 22) is outside<br>8 (20 20) is inside<br> 

(MAC OS X 10.6.3,PHP 5.3.1 CLI)

编辑:也许有会导致你的错误在于对其他部分你的脚本通过include/require调用你的类定义。

更多资讯可能有帮助。

+0

在运行PHP 5.3.1的CentOS上测试,我得到相同的输出。 – Zack 2010-05-15 17:32:27

+0

作者有问题;告诉他他遇到的问题不存在并不能解决问题。 – mattbasta 2010-05-15 17:34:44

+1

@mattbasta:其实它确实。通过证明它有效,我们可以向他保证,他发布的密码不是问题的原因。 – selfawaresoup 2010-05-15 17:37:07

0

测试样Techpriester,运行没有错误,并产生以下输出:

0 (30 19) is boundary<br>1 (0 0) is outside<br>2 (10 0) is vertex<br>3 (30 20) is vertex<br>4 (11 0) is boundary<br>5 (0 11) is boundary<br>6 (0 10) is vertex<br>7 (30 22) is outside<br>8 (20 20) is inside<br> 

测试在Ubuntu 9.10,Linux的2.6.31-17,PHP 5.3.2-0.dotdeb.2(CLI)

尝试检查您的php.ini是否有任何可能有该错误的auto-prepended files(auto_prepend_file)。

+0

此外,将这个相同的文件中检查以前包含(如果它们存在)将是一个好主意。 – 2010-05-15 17:45:06

0

由于代码适用于其他人,我试着将代码从Stackoverflow复制并粘贴回到我的系统中,并且工作正常。奇怪的。我一定有东西搞砸了格式。

我仍然不知道问题是什么,但至少它工作。

谢谢mattbasta,Techpriester和Zack。