From: Stéphane Jacob Date: Mon, 3 Jan 2011 12:20:22 +0000 (+0100) Subject: Improves Geocoder::getFirstLines to prevent warnings and unwanted \r. X-Git-Tag: xorg/1.0.2~43 X-Git-Url: http://git.polytechnique.org/?a=commitdiff_plain;h=256922ddf78534e571f4ce3b6c6885bb380a63c0;p=platal.git Improves Geocoder::getFirstLines to prevent warnings and unwanted \r. Signed-off-by: Stéphane Jacob --- diff --git a/classes/geocoder.php b/classes/geocoder.php index ccbddc7..89727a1 100644 --- a/classes/geocoder.php +++ b/classes/geocoder.php @@ -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.