|
#1
|
|||
|
|||
|
I would like to extract the latitude and logitude into separate variables from a string. If the string is defined like this, including whitespace:
$string = ' geocoder.us find the latitude & longitude of any US address - for free Address 6001 Savoy Dr Houston TX 77036 Latitude 29.716330 ° Longitude -95.505120 ° Search for another address: = geocoder.us = © 2004-5 Locative Technologies = terms & conditions = contact us = Acceptance Mark ' I've just read a breif tutorial at IBM Developerworks on regular expressions and here's my first attempt to get the latitude: PHP Code:
Here's my translation of the regx: Match Latitude at the begining of the string, followed optionally by any number of space characters, followed by zero or one - character, followed by any number, followed optionally by a literal dot, followed optionally by any number. How does this look? Any suggestions for improvement? Thanks! |
|
#2
|
|||
|
|||
|
It looks good to m. My only question is, is the information on the same line? The way you layed it out it looks like it is on separate lines which means you would have to use a regex with the /s option to convert the entire document into a single string (ignoring the line breaks).
Beyond that I don't see anything that jumps out as to why it would not work. Have you tested it? Any problems?
__________________
md |
|
#3
|
|||
|
|||
|
Quote:
The information is on separate lines. How would I use a regex with the /s option? I've tested it and it does not work. I've resorted to a couple strpos() operations in combination with a substr() to get it to work. Although it seems a little convoluted, it works and may be faster than a regx. If you'd like me to post my current solution let me know. Otherwise, I'm interested in any thoughts as to why the regx above may not be working, or on the relative merits of using a regx vs. a couple strpos() operations in combination with a substr(). Thanks for your reply. |
|
#4
|
|||
|
|||
|
I think you might have to use the preg functions to use the /s option and the other options but I am not 100% certain.
You could try PHP Code:
The other options are i - Case insensitive match. g - Global match (see below). e - Evalute right side of s/// as an expression. o - Only compile variable patterns once (see below). m - Treat string as multiple lines. ^ and $ will match at start and end of internal lines, as well as at beginning and end of whole string. Use \A and \Z to match beginning and end of whole string when this is turned on. s - Treat string as a single line. "." will match any character at all, including newline. x - Allow extra whitespace and comments in pattern.
__________________
md |
|
#5
|
|||
|
|||
|
Quote:
Thanks for the information. I'm not sure of the syntax and the php manual oddly doesn't give the details you've listed for preg_grep. I think eregi() is only for php5, for example. My host is using php 4.4.2. I'll give it a go though. |
|
#6
|
|||
|
|||
|
gearmelon--
Actually eregi has been in the PHP system since 3.0. If you read the manual for ereg you will see "Note: preg_match(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg()." Some of the preg functions started working in PHP as soon as 3.0.9 while others were not around until 4.0 so you will have to worry about that if you expect your users to use PHP 3.0. You can find all the information you need related to preg by going to the modifiers page and the pattern syntax page. Also note that while you are in the manual you are able to see all the preg related methods/functions and other information on the left hand side of the manual. I would highly suggest moving any regular expression checking to the preg functions as they are known to be faster and it also gives you the ability to do more complex searches.
__________________
md |
|
#7
|
|||
|
|||
|
Quote:
Right, I was thinking of strripos(), which is php 5 only. Great information. Regarding speed, I think the bottleneck in my application has more to do with its using file_get_contents($the_url) to open a page with parameters in the url to return latitude/longitude for a given address, and doing so in a loop for each address I'm trying to get coordinates for (typically 15-20 at a time) While working, it's quite slow. The page that's loading (with example params) is http://geocoder.us/demo.cgi?address...C+Washington+DC It seems the map and possibly the PayPal ad are slowing things up. ![]() |
|
#8
|
|||
|
|||
|
Are you doing a file_get_contents to your local machine? Not that it really matters but I would not think that would take a while.
When I tested it the site loaded really fast but, like you said, the map was what took about 15 to 20 seconds. I am not certain how you are generating the map but maybe there is a faster way?
__________________
md |
|
#9
|
|||
|
|||
|
Quote:
No, I'm saving file_get_contents to a variable ($contents), then using the aforementioned combo of strpos() and substr() to get at the latitude and longitude coordinates, then writing them to my database. Later, when I call up a Google map, I can retrieve the coordinates in order to place a marker. It's the Google map I'm after, not the geocoder map. The reason I'm using this site is to dynamically retrieve the coordinates for a given address in my database. To be clear, I'm sending the address info in the URL to the geocoder site, which gets me a page with lat/long. I couldn't do this with google.com A faster way, I would think, would be to access a page on the geocoder site without the map.He has both free and pay services as well as documentation for integrating with your appliation. The documentation says: Quote:
I've tried this and it's seems to be about the same speed, but my code is cleaner now. ![]() Last edited by gearmelon : 01-27-2006 at 06:26 AM. |
|
#10
|
|||
|
|||
|
I am trying to use a login script designed by a self-proclaimed PHP guru (maybe he is one) that gives me a hardtime when someone registers with a hyphenated "username" (i.e. nieves-riosr) or an email address that has hyphens "-" or periods "." (i.e. roberto.nieves-rios@abc.com).
For validation of the "username" the script has: Quote:
For validation of the email address the script has: Quote:
Maybe someone can review the above and suggest how can I change the script to solve my problem. Any guidance is appreciated. Thanks! |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|