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

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 = '';
$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.

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.
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;
Do the same calculation on the fly in PHP, something like:
$Venue = '';
That should do the trick ? ;-)

Related

Importing PHP string with quotes

So I'm importing ExpressionEngine fields into a php array. I want to display one field, called {gearboxx_body}, unless that field has more then 300 characters, in which case I want to display a field called {article_blurb}. I'm pretty sure there isn't a way to do this just in ExpressionEngine fields and conditionals, so I tried some PHP, which I'm just starting to learn:
<?php
$info = array('{gearboxx_body}','{article_blurb}');
if(mb_strlen($info[0]) <= 300)
echo($info[0]);
}
else {
echo($info[1]);
}
?>
So that works well, but there's a problem. If the tag includes any apostrophes or quote marks, it ends the string and the page won't load. So what can I do about this? I've tried to replace the quote marks in the string, but I have to have loaded the string from the fields first, and as soon as I do that the page is already broken.
Hopefully that made sense. Any suggestions?
I would recommend you handle this in an EE plugin rather than in the template:
Faster to render (because you don't need the overhead of PHP in the templates)
More secure and reliable
Faster to develop once you get the basics of EE development down which is a useful life skill
All around best-practice
The plugin I have in mind takes three parameters:
body, blurb and character limit.
Let's say you call your plugin "Blurby". In the template you would just have this:
{exp:blurby body="{gearboxx_body}" blurb="{article_blurb}" char_limit="300"}
It variably returns either of your fields based on the logic you define in the plugin itself.
See plugin developer documentation.
Alternatively you could use the dreaded HEREDOC syntax to set variables before passing them into your array:
$body = <<<EOT
{gearboxx_body}
EOT;
$blurb = <<<EOT
{article_blurb}
EOT;

str_replace URL space

I'm making a website where im alowing my users (after that they are loged in) to Add a (car) advertisement!
I have a form where the user can submit his car information.(add-vehicle.php)
Now I want to display each new advertisement in my list-view. (car-list.php)
How can I do this?
Use urlencode /urldecode to pass variables in url's urlencode
I recommand to use urlencode('string')
and then later when get your variable with urldecode('string')
Response to your comment:
if (isset($_GET['merk'],$_GET['car_id'],$_GET['titel']) === true )
{
$merk = urldecode(trim ($_GET['merk']));
$car_id = urldecode(trim($_GET['car_id']));
$titel = urldecode(trim($_GET['titel']));
}
You're changing a space into a hyphen. If it is stored in the database as a space, it will never find it because "This Entry" is different from "This-Entry". As others said, urlencode will work better, but if you still want to replace the space with a hyphen, just make sure that it is done the same in the database as well.
First, nobody in this world will know what you have in your database to tell what's the problem! At least post an example data.
Second, you must be sure of what you have and what you are comparing to.
You are basically asking if a is equal to b and to be fair that's something that you should be able to tell if you're programming!
Third, you should implement a methodology that allows you to quickly test your code, and that's from printing your data to the browser to a fully automated test.

preg_replace limit issue, handling array values

I've been working with the Sphider search engine for an internal website, we need to be able to quickly search for contact details in exported .htm(l) files.
$fulltxt = ereg_replace("[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*#[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,3})", "\\0", $fulltxt);
I am replacing e-mail addresses with a convenient mailto: link so users can open Outlook straight from the search results.
However,
while (preg_match("/[^\>](".$change.")[^\<]/i", " ".$fulltxt." ", $regs)) {
$fulltxt = preg_replace("/".$regs[1]."/i", "<b>".$regs[1]."</b>", $fulltxt);
}
It replaces all matches in the search results with bold tags, which resuts into the tags been included in Outlook's 'To...' field. It looks something like this in HTML (thanks Yuriy):
<b>name</b>.surname#domain
I have tried adding a value to the 'limit' parameter:
while (preg_match("/[^\>](".$change.")[^\<]/i", " ".$fulltxt." ", $regs)) {
$fulltxt = preg_replace("/".$regs[1]."/i", "<b>".$regs[1]."</b>", $fulltxt, 1);
}
Supposingly this should be the solution to my problem by simply replacing only the first occurrence (being the name as the pattern is name-phone num-email and we always search by name), instead it only makes it incredibly slow to the point i get a timeout message from the server. I've been trying various solutions but have been out of luck.
Any ideas? Am i doing something wrong?
Thanks.
(*Original heavily edited).
Did I understand you right that something like this happens?
<b>email#domain</b>
Why don't you put tags into search results first, and only then apply "mailto:" anchors to emails? Added 's would be easy to filter out in the patter on that second step.

Storing line break in a php Variable

I am developing a Facebook App in which I get some text from the Database and Display it on the Page.
The Problem is that I want to insert a line Break in the variable e-g
If I copy a Text from database and store it in a Variable..
Let say
$text="I love to walk";
I want to insert a line break after "to" how can i do that?
I had tried to store the text like this in html
"I love to <html> <br> </html> but that didn't worked..
Just suppose this is the Text ..may be next time the text is entirely Differnet having no "to" word.
Depends on if you want to create new line in code output, or in HTML
$nl = "\r\n";
$nl_html = "<br />";
That exmple you provided modify like this:
$lyrics = "I love to <br> but that didn't worked.."
To automatically add line break after some text, use preg_replace
$lyrics = preg_replace('/to /',"to<br />",$lyrics);
see http://php.net/manual/en/function.preg-replace.php
$new_str = str_replace('to', 'to <br />', $str, 1);
If you want to output the text in a html page, you need to make it
$text="I love to <br /> walk";
If you want to output it to a file you need to make it
$text="I love to\r\nwalk";
or
$text="I love to\rwalk"; depending on the OS on which you will be reading the file
Hi I was having issues with this just as you do earlier today (I am new to PHP).
The way I fixed this was as follows:
$format_text = nl2br($formatthis);
You would then refer to $format_text.
What it does is it keeps the line breaks.
However I am not quite sure what you mean with your OP, after re-reading it. I went by the topic and I answered it as best I could.
If you are having trouble let's say echoing html code then most definitely you are having trouble with escaping characters.
For instance:
echo "a href="something" /a this won't work.
echo "a href=\"something\" /a this will work, notice the .
you have two options either use preg_replace or use a variable to save the value
please see php documentation for further info

Post text from a text area

I have a form where a user types paragraphs into a text area and then it takes them to another page after they submit. How can I pass whatever they typed to the page after they submit? The text area might have linebreaks and if I use a query string to pass the data, it gives me an error. This is my current code to pass the field:
<?php
if(isset($_POST['form']))
{
$title = $_POST['title'];
$body = $_POST['body'];
header("SubmitForm.php?title=$title&body=$body");
?>
<html>
...html form...
It doesn't work when the text area has line breaks in it.
I would suggest installing a wysiwyg editor to make this easier for you, but i assume that would add some time for the learning curve.
The simplest tips I can give you is to set a CSS attribute for your textarea: white-space:pre so that when it gets submitted, all line breaks get sent as well.
On your server side, you would need to use the nl2br() function, so that when it gets saved on your DB or wherever you store them, all line breaks are converted to HTML breaks.
For your additional reference, I had a similar question like this last year.
You really shouldn't be putting anything that long in a query string in the first place. Look into using sessions to store data across pages instead.
(This is assuming I understood the question right)
urlencode the data in order to pass it via query string.

Categories