Convert Plain Text Address to clickable link to google maps via PHP

475 views Asked by At

Pulling in data from a Filemaker Pro database field and trying to convert the plain text data from the field into a clickable link to google maps via PHP.

My first attempt doesnt display anything when called:

$Venue = '<a href="https://maps.google.com/maps?q='.nl2br($record->getField ( 'Auctions::AIS_Venue' )).'"></a>';

$Venue is then echoed into a UL via

<?php echo $Venue; ?>

I'm relatively new to PHP so I'm sure there is a much more semantic way of marking this up? Possibly a regex and replace, returning a preg_replace? Which is what I've been using for plain text URLs and email addresses.

Anything helps, thanks so much.

1

There are 1 answers

1
Dycey On

Casey - not sure there's enough context here to help you? For instance, forget the link wrapping, does

<?php echo $record->getField( 'Auctions::AIS_Venue'); ?>

actually echo the field contents to a page?

Also, not sure you really want to use nl2br anyway, as you probably don't want a break in your url ;-)


OK, in which case there are a couple of things to try. First - can you get it to run in a webviewer inside FileMaker? I suspect not, and it may be down to an encoding/filtering issue.

There are two approaches you could use.

  1. Create a new calc field in FileMaker that filters out punctuation and converts spaces to '+'s, something like:

    Filter( Substitute( Lower( Auctions::AIS_Venue ) ; [ ΒΆ ; "+"] ; [" " ; "+"]; ["++" ; "+"]  ); "abcdefghijklmnopqrstuvwxyz1234567890+" )
    

and then use that field as in your PHP code, or;

  1. Do the same calculation on the fly in PHP, something like:

    $Venue = '<a href="https://maps.google.com/maps?q='.preg_replace( array("/[\n ]+/",'/[^a-zA-Z0-9+]/'), array('+',''), $record->getField ( 'Auctions::AIS_Venue' ) ).'"></a>';
    

That should do the trick ? ;-)