Improves Geocoder::getFirstLines to prevent warnings and unwanted \r.
authorStéphane Jacob <sj@m4x.org>
Mon, 3 Jan 2011 12:20:22 +0000 (13:20 +0100)
committerStéphane Jacob <sj@m4x.org>
Mon, 3 Jan 2011 12:20:22 +0000 (13:20 +0100)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
classes/geocoder.php

index ccbddc7..89727a1 100644 (file)
@@ -83,14 +83,23 @@ abstract class Geocoder {
     // and the city name, within the limit of $limit number of lines.
     static public function getFirstLines($text, $postalCode, $limit)
     {
-        $textArray  = explode("\n", $text);
+        $text = str_replace("\r", '', $text);
+        $textArray = explode("\n", $text);
+        $linesNb = $limit;
+
         for ($i = 0; $i < count($textArray); ++$i) {
             if ($i > $limit || strpos($textArray[$i], $postalCode) !== false) {
-                $limit = $i;
+                $linesNb = $i;
                 break;
             }
         }
-        return implode("\n", array_slice($textArray, 0, $limit));
+        $firstLines = implode("\n", array_slice($textArray, 0, $linesNb));
+
+        // Adds empty lines to complete the $limit lines required.
+        for (; $i < $limit; ++$i) {
+            $firstLines .= "\n";
+        }
+        return $firstLines;
     }
 
     // Returns the number of non geocoded addresses for a user.