reg expression help |
[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!!!
[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;
[eluser]Michael Ekoka[/eluser]
try this: preg_match_all('/-?[0-9]+(?:\.[0-9]+)?/' , $marker, $matches); $a = $matches[0][0]; $b = $matches[0][1];
[eluser]new_igniter[/eluser]
I tried that but got "Parse error: syntax error, unexpected '?'" Do you know why that is? Thank you very much!
[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);
[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.
[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)'; Copy and paste it and let me know what you get.
[eluser]Edemilson Lima[/eluser]
This page have many examples of Regular Expressions for many needs: http://regexlib.com/DisplayPatterns.aspx More info about RegEx: PHP - http://www.sitepoint.com/article/regular...ssions-php http://br.php.net/manual/en/ref.regex.php Javascript - http://www.sitepoint.com/article/expressions-javascript MySQ - http://dev.mysql.com/doc/refman/5.0/en/regexp.html Perl - http://www.pcre.org/ http://www.php.net/manual/en/book.pcre.php General - http://www.regular-expressions.info/ |
Welcome Guest, Not a member yet? Register Sign In |