CodeIgniter Forums
reg expression help - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: reg expression help (/showthread.php?tid=6897)



reg expression help - El Forum - 03-16-2008

[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!!!


reg expression help - El Forum - 03-16-2008

[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;


reg expression help - El Forum - 03-16-2008

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


reg expression help - El Forum - 03-16-2008

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


reg expression help - El Forum - 03-16-2008

[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];



reg expression help - El Forum - 03-16-2008

[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.


reg expression help - El Forum - 03-16-2008

[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.


reg expression help - El Forum - 03-16-2008

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


reg expression help - El Forum - 04-10-2008

[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-expressions-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/