• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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!

#9
[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/


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.