5

i am seriously struggling to get my head around regex.

I have a sring with "iPhone: 52.973053,-0.021447"

i want to extract the two numbers after the colon into two seperate strings so delimited by the comma.

Can anyone help me? Cheers

6 Answers 6

7

Try:

preg_match_all('/\w+:\s*(-?\d+\.\d+),(-?\d+\.\d+)/',
    "iPhone: 52.973053,-0.021447 FOO: -1.0,-1.0",
    $matches, PREG_SET_ORDER);
print_r($matches);

which produces:

Array
(
    [0] => Array
        (
            [0] => iPhone: 52.973053,-0.021447
            [1] => 52.973053
            [2] => -0.021447
        )

    [1] => Array
        (
            [0] => FOO: -1.0,-1.0
            [1] => -1.0
            [2] => -1.0
        )

)

Or just:

preg_match('/\w+:\s*(-?\d+\.\d+),(-?\d+\.\d+)/',
    "iPhone: 52.973053,-0.021447",
    $match);
print_r($match);

if the string only contains one coordinate.

A small explanation:

\w+      # match a word character: [a-zA-Z_0-9] and repeat it one or more times
:        # match the character ':'
\s*      # match a whitespace character: [ \t\n\x0B\f\r] and repeat it zero or more times
(        # start capture group 1
  -?     #   match the character '-' and match it once or none at all
  \d+    #   match a digit: [0-9] and repeat it one or more times
  \.     #   match the character '.'
  \d+    #   match a digit: [0-9] and repeat it one or more times
)        # end capture group 1
,        # match the character ','
(        # start capture group 2
  -?     #   match the character '-' and match it once or none at all
  \d+    #   match a digit: [0-9] and repeat it one or more times
  \.     #   match the character '.'
  \d+    #   match a digit: [0-9] and repeat it one or more times
)        # end capture group 2
Sign up to request clarification or add additional context in comments.

2 Comments

I'm wondering if it's really necessary to match digits? Might be far simpler to split at colon and comma? (pls excuse me if this is dumb ;-)
That is an option if the string will always look like that. But perhaps the input changes slightly or the matching is also used for validation.
2

A solution without using regular expressions, using explode() and stripos() :) :

$string = "iPhone: 52.973053,-0.021447";
$coordinates = explode(',', $string);
// $coordinates[0] = "iPhone: 52.973053"
// $coordinates[1] = "-0.021447"

$coordinates[0]  = trim(substr($coordinates[0], stripos($coordinates[0], ':') +1));

Assuming that the string always contains a colon.

Or if the identifier before the colon only contains characters (not numbers) you can do also this:

$string = "iPhone: 52.973053,-0.021447";
$string  = trim($string, "a..zA..Z: ");
//$string = "52.973053,-0.021447"

$coordinates = explode(',', $string);

Comments

0

Try:

$string = "iPhone: 52.973053,-0.021447";

preg_match_all( "/-?\d+\.\d+/", $string, $result );
print_r( $result );

Comments

0

I like @Felix's non-regex solution, I think his solution for the problem is more clear and readable than using a regex.

Don't forget that you can use constants/variables to change the splitting by comma or colon if the original string format is changed.

Something like

define('COORDINATE_SEPARATOR',',');
define('DEVICE_AND_COORDINATES_SEPARATOR',':');

Comments

0
$str="iPhone: 52.973053,-0.021447";
$s = array_filter(preg_split("/[a-zA-Z:,]/",$str) );
print_r($s);

Comments

0

An even more simple solution is to use preg_split() with a much more simple regex, e.g.

$str   = 'iPhone: 52.973053,-0.021447';
$parts = preg_split('/[ ,]/', $str);
print_r($parts);

which will give you

Array 
(
    [0] => iPhone:
    [1] => 52.973053
    [2] => -0.021447
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.