Open Source Development Tutorials | Newsletter Signup | About Us
Search  

Go Back   Open Source Tutorials Forum > Programming Languages > PHP Development
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 01-20-2006, 03:58 AM
gearmelon gearmelon is offline
Junior Member
 
Join Date: Jan 2006
Posts: 7 gearmelon is on a distinguished road
Default using eregi()

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:
 if (eregi('^[Latitude]([[:space:]])*(-)?[0-9]+(\.)*[0-9]*'$contents$catch))
{
 
$latitude $catch[1];


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!
Reply With Quote
  #2  
Old 01-22-2006, 09:44 AM
md_doc md_doc is offline
ubergeek
 
Join Date: Oct 2003
Posts: 204 md_doc has disabled reputation
Thumbs up

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
Reply With Quote
  #3  
Old 01-22-2006, 11:31 AM
gearmelon gearmelon is offline
Junior Member
 
Join Date: Jan 2006
Posts: 7 gearmelon is on a distinguished road
Default

Quote:
Originally Posted by md_doc
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?


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.
Reply With Quote
  #4  
Old 01-23-2006, 07:01 AM
md_doc md_doc is offline
ubergeek
 
Join Date: Oct 2003
Posts: 204 md_doc has disabled reputation
Default

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:
 preg_grep('/regext/s'$content); 


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
Reply With Quote
  #5  
Old 01-24-2006, 04:55 AM
gearmelon gearmelon is offline
Junior Member
 
Join Date: Jan 2006
Posts: 7 gearmelon is on a distinguished road
Default

Quote:
Originally Posted by md_doc
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:
 preg_grep('/regext/s'$content); 


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.


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.
Reply With Quote
  #6  
Old 01-24-2006, 07:11 AM
md_doc md_doc is offline
ubergeek
 
Join Date: Oct 2003
Posts: 204 md_doc has disabled reputation
Default

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
Reply With Quote
  #7  
Old 01-24-2006, 11:07 AM
gearmelon gearmelon is offline
Junior Member
 
Join Date: Jan 2006
Posts: 7 gearmelon is on a distinguished road
Default

Quote:
Originally Posted by md_doc
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.


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.
Reply With Quote
  #8  
Old 01-26-2006, 08:44 AM
md_doc md_doc is offline
ubergeek
 
Join Date: Oct 2003
Posts: 204 md_doc has disabled reputation
Default

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
Reply With Quote
  #9  
Old 01-27-2006, 05:08 AM
gearmelon gearmelon is offline
Junior Member
 
Join Date: Jan 2006
Posts: 7 gearmelon is on a distinguished road
Default

Quote:
Originally Posted by md_doc
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?


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:
The absolute simplest way to get a geocode from the free service is to use the csv interface. Entering this in your browser:

http://rpc.geocoder.us/service/csv?...,+Washington+DC

Returns this result:

38.898748,-77.037684,1600 Pennsylvania Ave NW,Washington,DC,20502

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.
Reply With Quote
  #10  
Old 12-04-2007, 12:22 PM
cangrejero cangrejero is offline
Junior Member
 
Join Date: Dec 2007
Posts: 1 cangrejero is on a distinguished road
Default I've read; I've tried; I've failed!

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:
$req_user = trim($_GET['user']);
if(!$req_user || strlen($req_user) == 0 ||
!eregi("^([0-9a-z])+$", $req_user) ||
!$database->usernameTaken($req_user)){
die("Username not registered");


For validation of the email address the script has:
Quote:
/* Check if valid email address */
$regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
."\.([a-z]{2,}){1}$";


Maybe someone can review the above and suggest how can I change the script to solve my problem. Any guidance is appreciated. Thanks!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump



All times are GMT -5. The time now is 07:21 AM.


Powered by: vBulletin Version 3.0.1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright 2004 - 2006 GrindingGears.com. All Rights Reserved.