PHP: how to add double quote to a PHP variable - php

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.

Related

Replace string with 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');

Escaping minus sign in PHP echo statement

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>';

How do you show double quotes in single quotes PHP

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.

Quotes problem when passing value to Javascript

I am using like
$myPage .= '<td><a href=\'javascript:editProduct('
.$row['id']
.',"'
.$row['name']
.'")\'>Edit</a></td>';
where $row['name'] has quotes in its value. it breaks. how do i solve the issue both from php side and js side...
$row['name'] is value from DB. and it will have value like pradeep's and pradeep"s also
i used like
$myPage .= '<td><a href=\'javascript:editProduct('.addslashes($row['id']).',"'.addslashes($row['name']).'")\'>Edit</a></td>';
it solves the issue of double quotes. but when i have single quotes in value the javascrit link looks like
javascript:editProduct(28,"pradeep\
it actually breaks..
And how do i strip down the slashes added by addslashes in javascript..
UPDATE - FINAL CODE
$myPage .= '<td><a href=\'javascript:editProduct('.$row['id'].',"'.htmlentities($row['name'],ENT_QUOTES).'")\'>Edit</a></td>';
and js looks like
function editProduct(id,name){
alert(name);
}
can any one solve my issues
Try:
$myPage .= "<td><a href='javascript:editProduct({$row['id']},\""
. htmlentities( $row['name'] )
. "\")'>Edit</a></td>";
htmlentities default behaviour is to convert double quotes and leave single quotes alone, if you require converting single and double quotes, then call it like this:
htmlentities( $row[ 'name' ], ENT_QUOTES )
Also, using { .. } in "..." strings is the correct way to substitute variables.
The PHP string
'<a href=\'javascript:editProduct('.$row['id'].',"'.$row['name'].'")\'>';
outputs (assuming some values)
<td><a href='javascript:editProduct(123,"abc")'></td>
Presumably it breaks if $row['name'] contains a " quote. You could replace such quotes with a \" in the string before you output it using str_replace('"', '\"', $row['name'])

Wrapping quotes around a php id in XML

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

Categories