diff --git a/src/ShapeFile.php b/src/ShapeFile.php index 33932df..6a40107 100644 --- a/src/ShapeFile.php +++ b/src/ShapeFile.php @@ -226,15 +226,15 @@ public function setDBFHeader($header) { * @return integer */ public function getIndexFromDBFData($field, $value) { - $result = -1; - $count = count($this->records) - 1; - for ($i = 0; $i < $count; $i++) { - if (isset($this->records[$i]->DBFData[$field]) && (strtoupper($this->records[$i]->DBFData[$field]) == strtoupper($value))) { - $result = $i; + foreach ($this->records as $index => $record) { + if (isset($record->DBFData[$field]) && + (trim(strtoupper($record->DBFData[$field])) == strtoupper($value)) + ) { + return $index; } } - return $result; + return -1; } private function _loadDBFHeader() { diff --git a/tests/ShapeFileTest.php b/tests/ShapeFileTest.php index d26a14d..658b444 100644 --- a/tests/ShapeFileTest.php +++ b/tests/ShapeFileTest.php @@ -378,4 +378,21 @@ public function shapes() ), ); } + + public function testSearch() + { + $shp = new ShapeFile(0); + $shp->loadFromFile('data/capitals.*'); + /* Nonexisting entry or no dbase support */ + $this->assertEquals( + -1, + $shp->getIndexFromDBFData('CNTRY_NAME', 'nonexisting') + ); + if (ShapeFile::supports_dbase()) { + $this->assertEquals( + 218, + $shp->getIndexFromDBFData('CNTRY_NAME', 'Czech Republic') + ); + } + } }