Welcome Guest, Not a member yet? Register   Sign In
reg expression help
#1

[eluser]new_igniter[/eluser]
Hello,
can a reg expression guru help me out here?

I have a Google Maps address:
(47.718691, -122.28464)

How do I
a) get rid of the parentheses on the left
b) split the values at the comma into 2 variables
c) get rid of the closing parentheses on the right

so I end up with,
$a = 47.718691
$b = -122.28464

Thanks!!!
#2

[eluser]new_igniter[/eluser]
I did this. works fine, wondering if there is a better way:

$marker = "(47.718691, -122.28464)";
$newstring = substr($marker, 1, -2);
list( $a,$b ) = split( ",", $newstring, 2 );
echo $a;
echo "<br />";
echo $b;
#3

[eluser]Michael Ekoka[/eluser]
try this:
preg_match_all('/-?[0-9]+(?:\.[0-9]+)?/' , $marker, $matches);
$a = $matches[0][0];
$b = $matches[0][1];
#4

[eluser]new_igniter[/eluser]
I tried that but got "Parse error: syntax error, unexpected '?'" Do you know why that is? Thank you very much!
#5

[eluser]Michael Ekoka[/eluser]
I see. The script that handles these forum's post changed my single quotes into forward ticks. I should have enclosed it in code tags. Here it is again, untransformed:
Code:
preg_match_all('/-?[0-9]+(?:\.[0-9]+)?/' , $marker, $matches);
$a = $matches[0][0];
$b = $matches[0][1];
#6

[eluser]new_igniter[/eluser]
I think what I did is not include that I the "(" is actually in my string.

so "(47.718691, -122.28464)" is the actual string. I can't seem to get a value.

Thanks so much for your help. I am very uncomfortable with regular expressions.
#7

[eluser]Michael Ekoka[/eluser]
I'm not sure that I understood what you just tried to say. Are you able to grab the 2 values or are you still having a problem? Your code should look like this:
Code:
$marker = '(47.718691, -122.28464)';
preg_match_all('/-?[0-9]+(?:\.[0-9]+)?/' , $marker, $matches);
$a = $matches[0][0];
$b = $matches[0][1];

Copy and paste it and let me know what you get.
#8

[eluser]new_igniter[/eluser]
my bad, works perfect. thanks for the help!




Theme © iAndrew 2016 - Forum software by © MyBB