I have a PHP echo statement:
echo "stores[".$row['BarID']."] = [". $row['BarName'] . ", " . $row['Address']. ",". $row['City']. "," . $row['State']. " 0". $row['ZipCode']. "," . $row['PhoneNumber']. ",". $row['Lattitude']. ",".$row['Longitude']. "]". ";<br>";
which outputs:
stores[0] = [The Ale 'N 'Wich Pub , 246 Hamilton St ,New Brunswick,NJ 08901,732-745-9496 ,40.4964198,-74.4561079];
BUT I WOULD LIKE THE OUTPUT IN DOUBLE QUOTES SUCH AS:
stores[0]=["The Ale 'N 'Wich Pub", "246 Hamilton St, New Brunswick, NJ 08901", "732-745-9496 Specialty: Sport", "40.4964198", "-74.4561079"];
I Have looked at the PHP String Functions Manual on PHP site but still don't understand how i can implement it. Your help is appreciated.
The keyword you miss is "escaping" (see Wiki). Simplest example:
echo "\"";
would output:
"
EDIT
Basic explanation is - if you want to put double quote in double quote terminated string you MUST escape it, otherwise you got the syntax error.
Example:
echo "foo"bar";
^
+- this terminates your string at that position so remaining bar"
causes syntax error.
To avoid, you need to escape your double quote:
echo "foo\"bar";
^
+- this means the NEXT character should be processed AS IS, w/o applying
any special meaning to it, even if it normally has such. But now, it is
stripped out of its power and it is just bare double quote.
So your (it's part of the string, but you should get the point and do the rest yourself):
echo "stores[".$row['BarID']."] = [". $row['BarName'] . ", " . $row['Address'] .
should be:
echo "stores[".$row['BarID']."] = [\"". $row['BarName'] . "\", \"" . $row['Address']. "\"
and so on.
Related
When i echo "$time"; - The output is 2015-07-27 18:17:47
But i need to output as "2015-07-27 18:17:47".
I have been trying various string concatenations such as : echo "."$time"."; But couldn't get the desired output? What is the best way to do it?
Try this concatenation
echo '"' . $time . '"';
or use printf() like so
printf('"%s"', $time);
Just escape them:
echo "\"$time\"";
You could also use single around the double quotes:
echo '"' . $time . '"';
See here for more info on escape sequences when using double quotes.
I'm trying to replace strings having no quotes+HTML tag with those having quotes.
Example: worlds in <i>worlds<i> to be replaced with World's. So, <i>worlds<i> becomes World's.
I'm using the following code, but it doesn't take into account the '(quotes).
preg_replace('/\b' . preg_quote('worlds') . '\b/i', '<i>$0</i>', 'World's');
you have to escape the ' by placing \ before. Try this:
preg_replace('/\b' . preg_quote(worlds) . '\b/i', '<i>$0</i>', 'World\'s');
I'm sure I'm missing something obvious here: the following is echoing lat-long variables from MySQL, and the longitude variable begins with a minus sign, which prevents the echo statement from reading it and all that follows it. I'm sure there is a way to clean/escape that but just can't work it out.
echo "http://maps.google.com/maps?ll=" . $row['latitude'] . "," . $row['longitude'] . " target=_new>View in Google Maps";
This is output from a PDO query and testing passing the lat-long into Google Maps.
As I understand, it's a link?
Then, use urlencode for string.
The minus signs are not a problem. You may need to urlencode() because of the comma, but you need quotes around the URL in the href as well:
echo '<br /><a href="http://maps.google.com/maps?ll='
. urlencode($row['latitude'] . ',' . $row['longitude'])
. '" target="_new">View in Google Maps</a>';
I'm having some trouble with writing some syntax. I want to echo this
'location_1' => $location_1,
However, it is not as simple as it seems. When I write the echo statement the integer 1 must be the variable $z. Here is the code I attempted to write
echo "'location_' . $z . '' =>' . ${'location_' . $z} . ','";
This is what it outputted
'location_' . 1 . '' =>' . something . ','
$location_1 is equal to the string something. I'm lost at how to do this the right way. Any guides on describing how this syntax works would be a major help too so I can understand it completely.
You can just write variables directly into double quoted strings see http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing
echo "'location_$z' => \$location_$z,";
You might want to also read the rest of the strings doc
This is the link to the echo documentation (see the examples, I think they described well how it works)
You can break it into two lines and get the expected output.
For example:
$var_location = "$". "location". $z;
echo "'location_" . $z . "' =>'" . $var_location . "','";
One way is: echo "'location_{$z}' => \$location_{$z},";
Edit: Is this what you meant?
<?php
$z = 1;
$location_1 = 'something';
echo "'location_$z' => " . ${'location_'. $z} . ',';
which produces: 'location_1' => something,
Why don't you store these variables inside an array for easier access. Something like:
$locations = array('location_id' => 'location_name');
Here's one way:
echo "'location_$z' => \$location_$z,";
You need to escape the $ symbol. The double quotes represent the thing to echo in this case, whereas the single quotes actually get echoed.
I've got this line of code:
$xml_output .= "\t<Event=" . $x . ">\n";
And it will output:
<Event=0>
<Event=1>
<Event=2>
etc etc through my loop.
I need it to output as this (with the quotes around the number):
<Event="0">
<Event="1">
<Event="2">
Any help, and I'm sure it's simple would be greatly appreciated!
$xml_output .= "\t<Event=\"" . $x . "\">\n";
PHP Strings
To specify a literal single quote, escape it with a backslash (\). To specify a literal backslash before a single quote, or at the end of the string , double it (\\). Note that attempting to escape any other character will print the backslash too.
$xml_output .= "\t<Event=\"" . $x . "\">\n";
However, that is not valid xml.
$xml_output .= "\t<Event=\"" . $x . "\">\n";
The slash escapes the quote for output. More information about double quoted strings can be found in the PHP manual