homepage Welcome to WebmasterWorld Guest from 54.197.110.151
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Making Street Names user-friendly
webfoo

5+ Year Member



 
Msg#: 4289361 posted 4:30 am on Mar 30, 2011 (gmt 0)

I'm working on an application utilizing the Google Maps Directions API. It is working fairly well. However, one of the issues I am having involves making the street names more readable to humans.

Right now, the HTML for the turn-by-turn directions looks like this, for example:
<div>1. Turn <b>Left</b> at <b>E Main St/State Route 1033</b></div>
<div>2. Turn <b>Right</b> at <b>State Route 670/Elm St</b></div>
<div>3. Take the 1st <b>Left</b> onto <b>Jefferson Ave</b></div>
<div>4. Turn <b>Right</b> onto <b>US-81</b></div>...

Rather than saying E Main St/State Route 1033, it simply needs to say E Main St. The users of this application don't care what the road number is.

To further complicate the issue:
- sometimes the road name comes first (ie. step 1 above)
- sometimes the road number comes first (ie. step 2 above)
- sometimes there is only a road name (ie. step 3 above)
- sometimes there is only a road number (ie. step 4 above)

The only part that is really consistant is that the street names are always in the second <b> tag of each step in the directons.

What's the best way to simplify the street name? Keep in mind that each of these <div>s are SimpleXML objects in the PHP code.

 

Finger

10+ Year Member



 
Msg#: 4289361 posted 11:14 am on Mar 31, 2011 (gmt 0)


// example
$line = "<div>1. Turn <b>Left</b> at <b>State Route 1033/E Main St</b></div>";
// if you have each line isolated you could use a regular expression to isolate the last bold section
preg_match_all("/<b>([^<]+)<\/b>/",$line,$matches);
$lastval = count($matches[1]) - 1;
$street_data = $matches[1][$lastval];

// If you want to remove the extra part with numbers, if it exists, you could do this:
if ( strstr($street_data,"/") ) {
// split the parts
$parts = explode("/",$street_data);
// test to see if the part does not end with a number, if it doesn't, then we set $street_name to that value
if ( !preg_match("/[0-9]+$/",$parts[0],$matches) ) {
$street_name = $parts[0];
} else if ( !preg_match("/[0-9]+$/",$parts[1],$matches) ) {
$street_name = $parts[1];
} else {
// just in case they somehow both end with a number we just set $street_name to the first value
$street_name = $parts[0];
}
} else {
// if theres no "/" character then $street_name is set to the original string
$street_name = $street_data;
}


I would suggest keeping the extra data because it might still be useful to some people, but just put it in parenthesis or something to make it more readable.
Since you already have the parts isolated, you could just do something like $street_name = $parts[0] . " (" . $parts[1] . ")";

webfoo

5+ Year Member



 
Msg#: 4289361 posted 7:06 pm on Mar 31, 2011 (gmt 0)

Thanks for the insight, Finger! This proved to be a good start. I had to make one change. The Route Numbers don't always end in a number. For example, "State Route 846 W", where the W stands for Westbound. Therefore, I changed the pregex match to look for strings containing a number, not just ending with one.

The other change I made is simply renaming a variable. In Google Direction's XML results, the element name for the words describing each step is <html_instructions>. Therefore, I had previously named the variable containing that data to $html_instructions instead of $line. No big deal.


For the record, the "final" code I used is as follows. "Final" in quotes because I will probably be back on here tomorrow with another problem :-)

preg_match_all("/<b>([^<]+)<\/b>/",$html_instructions,$matches); // Find all bold contents
$lastval = count($matches[1]) - 1; // Identify the last bold string
$street_data = $matches[1][$lastval]; // Set that value to $street_data

if ( strstr($street_data,"/") ) { // If $street data contains a slash
$parts = explode("/",$street_data); // Split $street_data at the slash
if ( !preg_match("/[0-9]/",$parts[0],$matches) ) { // test to see if the part does not contain a number, if it doesn't, then we set $street_name to that value
$street_name = $parts[0];
}
else if ( !preg_match("/[0-9]/",$parts[1],$matches) ) {
$street_name = $parts[1];
}
else { // just in case they somehow both contain a number we just set $street_name to the first value
$street_name = $parts[0];
}
}
else { // if theres no "/" character then $street_name is set to the original string
$street_name = $street_data;
}

webfoo

5+ Year Member



 
Msg#: 4289361 posted 8:49 pm on Mar 31, 2011 (gmt 0)

As promised, I am back with another issue :-)

Overall it is working very well. The issue now is that the <b> containing the road name/number isn't always the last <b> tag. For example:

Exit onto <b>Route 625/W Main St</b> toward <b>Huntsville</b>


So, the <b> we are looking for could be any one, not necessarily the last one. Therefore, the code must be modified to look for the <b> containing the slash character.

Ideas?

webfoo

5+ Year Member



 
Msg#: 4289361 posted 6:55 pm on Apr 4, 2011 (gmt 0)

To fix the issue mentioned in the above post, I decided loop through all <b> tags in the direction step. Then, for each <b> tag, see whether it contains a slash. If it does, explode it around the slash, and check each part to see if it contains a number. Set the part without the number to $street_name.

This works in concept, but something is the matter and it's not changing the contents of any <b> tags. Anyone spot errors off the cuff on this?

preg_match_all("/<b>([^<]+)<\/b>/",$html_instructions,$matches); // Find all bold contents

foreach($matches as $match) { // for each Bold tag

if( stripos($match,"/") ) { // if $match contains a slash

$parts=explode("/",$match); // Break it apart at the slash

if ( !preg_match("/[0-9]/",$parts[0],$match) ) { // If the part BEFORE the slash doesn't contain a number
$street_name=$parts[0];// use it as the street name
}

elseif ( !preg_match("/[0-9]/",$parts[1],$match) ) { // if the part AFTER the slash doesn't contain a number
$street_name=$parts[1]; // use it as the street name
}

else { // In case BOTH parts contain a number
$street_name=$parts[0]; // Just use the part before the slash
}

}
else { // If no slash was found in the $street_data
$street_name=$match; // put the $street_data through into the $street_name
}

$html_instructions=str_replace($match,$street_name,$html_instructions); // Puts the resolved street name in place of the complicated string

} // end foreach

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved