开发者

preg_match coordinates with degree sign

I am tryin开发者_如何学编程g to capture the coordinates of an input that could look like this

22°50'23.46"S, 43° 0'44.80"W

or this

41°12'27.84"N, 16°18'40.15"E

I think the problem is the degree symbols in my pattern. My current code:

$found = preg_match("~(\d+\p{Sm}\d+\s*\'\s*\d+\s*\.\s*\d+\s*\"\s*[N|S])\s*,\s*(\d+\s*\p{Sm}\s*\d+\s*\'\s*\d+\s*\.\s*\d+\s*\"\s*[E|W])~i", $_POST['submission']['url'], $matches);

$matches stays empty.


You're missing the /u modifier to make the engine interpret the pattern as Unicode.


What is stopping you from using the ° character literally?

/
  (\d+\s*°\s*  \d+\s*'\s*  \d+\s*\.\s*\d+\s*"\s*  [NS])
  \s*,\s*
  (\d+\s*°\s*  \d+\s*'\s*  \d+\s*\.\s*\d+\s*"\s*  [EW])
/xi


Perhaps you should try ignoring the actual degrees and tick marks and focus just on the data you want to grab.

(\d+)\D+(\d+)\D+(\d+)\.(\d+)\W(\w+)\W+(\d+)\D+(\d+)\D+(\d+)\.(\d+)\W(\w)

This regex should capture the following data:

41
12
27
84
N
16
18
40
15
E

(Edit2: Corrected to properly pick up the N without punctuation.)


Use mb_ereg_match instead to support UTF-8 chars. Docs: http://php.net/manual/en/book.mbstring.php

Initialize mb* like this:

mb_regex_encoding('UTF-8');
mb_internal_encoding('UTF-8');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜