Double Quotes shown as some strange characters - PHP, HTML - php

If you notice the two line below, the double quotes are not the same. the first one is what i have a problem with. They are shown as strange characters like - �. But the secound line double quotes is just fine.
“this is line 1.”
and
"this is line 2."
What is the difference between the two double quotes, and how can the special characters be prevented?

You should make sure, your PHP script uses utf-8, as well as the html meta tag says utf-8.
For the first thing, try in PHP (before any output occurs)
header('Content-Type: text/html; charset=utf-8');
In php, you can escape most HTML specialchars with "htmlentities". See http://php.net/manual/de/function.htmlentities.php

First line you copied probably from MS word/MS Excel. Their double quotes are different and will not parse properly using HTTP. You need to convert them using UTF-8 charset and then display on your website.

In line 1, those quotes are sometimes called "smart quotes". They are ascii code #147 and #148.
In line 2, those are "normal" quotes, ascii code #34.
Because character definition beyond ascii code #127 can become somewhat arbritrary depending on the font used, I try to avoid using the smart quote characters.
Micorosoft Word will (infamously) convert normal quotes to "smart quotes". This "feature" can be turned off in settings.

This issue occurred to me when I copy pasted text text from word document to label. If you observe carefully Word document double quotes looked little curvy opposed to HTML double quotes. Just removing copy pasted doubled quotes and typing again helped. ” - This from Word ." - This is from HTML . You can see the difference yourselves

Related

How do I properly escape double quotes in strings nested in a JSON object located in a CSV column?

I have a use case where a customer needs to load JSON-serialized objects via a CSV import. Some of these objects contain strings which contain double-quotes. Typically I would simply add a '\' before the nested double-quote in order to escape it, however this seems to conflict with the parsing of the CSV file. We're using PHP 7.0 and the function "fgetcsv" to read the lines of the file. Whenever I do this I notice odd behavior after an escaped double-quote is encountered. Here's a sample row from the CSV:
"{""test"": ""\""this\"" is a test""}"
And here is how PHP reads this column using fgetcsv:
{"test": "\"this\"" is a test""}"
I have confirmed any double-quotes after the initial escaped double-quote run into this problem. Thinking the backslash may be causing issues with escaping I tried using another backslash to escape the backslash:
"{""test"": ""\\""this\\"" is a test""}"
And here's the result:
{"test": "\\"this\\" is a test"}
So while this does resolve the issue with any double-quotes beyond the first, I am left with two backslashes instead of one.
Without changing the underlying code, is there a way to escape this data so that fgetcsv will interpret it appropriately? Like so:
{"test": "\"this\" is a test"}
You could try using \" to represent a double quote instead of "".
E.g., "{\"test\": \"\\\"this\\\" is a test\"}"
Whether this works may depend on the version of fgetcsv you are using -- I'm not sure.
Alternatively, if you're using fgetcsv 5.3 or later, you could try changing the fgetcsv parameters to change the enclosure character or escape character so that it doesn't conflict with JSON. See the parameters in the fgetcsv docs.
enclosure
The optional enclosure parameter sets the field enclosure character (one character only).
escape
The optional escape parameter sets the escape character (one character only).
Note: Usually an enclosure character is escaped inside a field by doubling it; however, the escape character can be used as an alternative. So for the default parameter values "" and \" have the same meaning. Other than allowing to escape the enclosure character the escape character has no special meaning; it isn't even meant to escape itself.
(emphasis in original)

PHP echos odd characters ("“helloâ€") on OS X installation

This is my first time with MAC on php, earlier I have been using ubuntu machines for PHP.
I have successfully installed MAMP. and now I have a file(index.php) in htdocs
<?php
echo “hello”;
and its output on safari is :
“helloâ€
I have tried with several different texts, all of them generate absurd output on browser.
Where is the problem in mac, safari or MAMP ?
update :
Without curly quotes the output is ‘hello’
Don't use special quotes like “ and ” but use " or ' instead. Be sure to use a simple text editor when you write code and not something like Word, for example, which will replace the simple quote characters to more fancy ones.
PHP doesn't understand the fancy quotes and won't substitute them with " or ', which have a special meaning in the language.
So, why didn't it break? PHP is incredibly forgiving, which has advantages and disadvantages. Consider the following code, which uses a constant:
define('HELLO', 'Hello world!');
echo HELLO;
This works and will output "Hello world!"
Now, if we pass what looks like a constant to PHP but don't define it, PHP will just output the (inexistent) constant's name instead:
echo HELLOWORLD;
this will output "HELLOWORLD".
The same happens with the text bit “hello” – it tries to look for a constant with that name, doesn't find any, and so just outputs “hello”. This only outputs an E_NOTICE error, which may be disabled by default. It is recommended to output all errors during development to avoid errors such as this:
error_reporting(E_ALL);
echo “hello”;
This will output:
Notice: Use of undefined constant “hello†- assumed '“helloâ€'
And indeed, if we tried to add a space between those special quotes, it would fail as constants can't have a space in their name, and so the text can't be interpreted at all:
echo “hello world”;
Parse error: syntax error, unexpected 'worldâ€' (T_STRING), expecting ',' or ';'
As to why the special quotes like “ aren't being displayed properly, this is because of an encoding problem. “ has an ASCII value above 127 and so can be interpreted in different ways depending on the encoding. Your file is saved in a certain encoding but your server and browser may assume it is in another one, yielding false characters.
you must set your docmuent as utf-8 encode.
method-1: add following html code to your page
<meta charset="utf-8" />
method-2: using php code like below
<?
header("Content-Type: text/html;charset=utf-8");
echo “hello”;
?>
the header must be in the first line of your code.
it will output “hello” ; if you want to output hello please change your double-quote to 'hello'or "hello". because your current double-quote are not english.
You can quote with quote ', double-quote ".
Your double quotes are supposed to be " not “ or ”.
Your Problem With encoded You Must Make It UTF-8 (utf8_encode)
string utf8_encode ( string $data )
Note If Not Work That Mean Problem With Your Pc or Browsers Then Go To Langs In Control panal..
Hope Hear Your Answer...

PHP output with double quotes

I have the following value from a field in a database that I would like to output exactly like this...
Use the term “fully accredited”-there is no such thing as a partial accreditation.
I need to include the quotes around the term "fully accredited".
Here's my output in PHP...
echo "<p><strong>Never:</strong> <span id=\"nevermsg\">".$results['never1']."</span></p>";
But, when I render the data on the page, it's showing these little diamond shapes with question marks inside them.
*(The 'span id' is there for styling and isn't relevant)
I don't think escaping would work here because quotes are not used in all the data values.
Not sure what to do...
The quotes in your string are extended characters.
You could fix this problem pretty quickly by simply replacing them with standard " quote characters rather than the curly quotes “ ” you've got now.
However, in the long term, you probably need to be able to handle extended characters, as it includes all kinds of things you're likely to need in your text, not just curly quote marks.
To fix this problem properly, you need to ensure that your system uses UTF-8 encoding at all levels. This includes within the database, your PHP code files, and the data that is sent to the browser.
I suggest reading up further on this here: UTF-8 all the way through

Does sometime fputs() or fwrite() encode html special characters?

I am outputting a string that consists of html content to a html file, but in the html file the html special characters are encoded (for example " in \" ). I've even used htmlspecialcharacters_decode before using the write functions. The wierd part is that on my computer the characters are not encoded, while uploaded on some server are encoded. How can I deal with this problem?
Anticipated thanks!
You are probably suffering from Magic Quotes
Check you phpinfo();
To clear Magic Quotes look into the discussion at php.net:
http://www.php.net/manual/en/function.stripslashes.php
Example (c) jeremysawesome:
array_walk_recursive($_POST, create_function('&$val', '$val = stripslashes($val);'));

Special chars in single and double quoted strings

I fetch a field from a database that contains a rtf document.
For Example this could look like this:
{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
{*\generator Msftedit 5.41.21.2509;}\viewkind4\uc1\pard\sa200\sl276\slmult1\lang7\f0\fs22 asdfasdf\par
a\par
sf\par
asd\par
fasd\par
\b dfas\b0\par
dfas\par
}
Now PHP fetches this as double quoted from the database, the result ist that the string will not be interpreded char wise... assumed special chars like '\r' and '\n' got recognized.
How can i convert from this double quoted to a single quoted string so that i got all raw chars? Or how can i achieve that the value is asigned as single quoted when i fetch it from database?
Thanks in advance
-ralf
Now PHP fetches this as double quoted
from the database
What? The result of mysql_fetch_row or whatewer is just a string. Nothing is reinterpreted in any way. \n just stays \n. Only string literals you write in the PHP file into double quotes will be "interpreted" and then stored as a string.
There is nothing like single- or double-quoted string. There are just single- or double-quoted string literals in the PHP source code from which the actual PHP strings will be made.
The only problem you have now is how to process/parse the RTF data. (Assuming the data was stored in blob column so there is no complication with character encodings.)
First of all you should invest some time who (or what) is escaping your code.
But for a quick solution, try to use the stripslashes() function:
$unsecaped = stripslashes( $database_data );
But I urge you try to find what is escaping the data.
This can occur:
Before inserting the data into database. This is typically caused by the PHP directive magic_quotes_gpc.
When retrieving the data from database.
Updated
I didn't understand your problem...
You want to keep all those backslashes but avoid to \r and \n being interpreted as carriage return and line feed...
Try to do a str_replace to find all those \r and \n and replacing them with \r and \n.
I don't know if \r could belong to any wise char, so maybe you should replace only " \r
"/" \n ", You'll need preg_replace() for this possibly.

Categories