Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
PHP では、次のような緯度/経度を含む文字列があります。(38.889373, -77.00279599999999)
(38.889373, -77.00279599999999)
を返し、最初の数値 (緯度) を抽出したい38.889373。これとは別に、2 番目の数値 (lng) を抽出して を返したいと思い-77.00279599999999ます。
38.889373
-77.00279599999999
正規表現は毎回私のお尻を蹴ります。助けてください!
みんなの提案にもかかわらず、正規表現が最も簡単な方法だと本当に思います。文字列/行ごとに 2 つの数値 (緯度と経度) しかないと想定できると思います。
preg_match_all("/[\d.-]+/", $str, $matches);
ペアの配列 (複数の緯度/経度セットがある場合) は にあり$matches[0]ます。を使用できますarray_chunk($matches[0], 2)。
$matches[0]
array_chunk($matches[0], 2)