Created
April 4, 2016 02:08
-
-
Save rodorgas/db0128db300445a6eb5dee9fe6319339 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// http://pt.stackoverflow.com/a/121085/6267 | |
// Método 1 - um regex bem massa | |
$poligono = array(); | |
$pontos_str = "(-22.891996181150216, -43.127254512695345), (-22.86529776201615, -43.09548266210936)"; | |
if (preg_match_all('#\([-,+][0-9\.]+, [-,+][0-9\.]+\)#', $pontos_str, $pontos) > 0) { | |
foreach ($pontos[0] as $ponto) { | |
$poligono[] = str_replace(' ', '', $ponto); | |
} | |
$poligono[count($poligono)] = $poligono[0]; | |
} | |
// Método 2 - possivelmente mais rápido, só que menos legível | |
$poligono = array(); | |
$pontos_str = "(-22.891996181150216, -43.127254512695345), (-22.86529776201615, -43.09548266210936)"; | |
$pontos = explode(',', str_replace(' ', '', $pontos_str)); | |
for ($i=0; $i < count($pontos); $i++) { | |
if ($i % 2 == 0) { | |
$poligono[] = $pontos[$i]; | |
} | |
else { | |
$poligono[count($poligono) -1] .= ','.$pontos[$i]; | |
} | |
} | |
if (count($poligono) > 0) | |
$poligono[count($poligono)] = $poligono[0]; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment