How do I remove quotation from csv?
Code
use Goutte\Client;
$client = new Client();
$response = $client->request('GET', 'http://c-manage.herokuapp.com/login');
$login_form = $response->filter('form')->form();
$login_form["account"] = '1';
$login_form["password"] = 'rpa1001';
$client->submit($login_form);
$client->request('GET', 'http://c-manage.herokuapp.com/client/download?searchQuery%5Bstatus%5D=1&searchQuery%5BregisterStartDate%5D=2010-01-01&searchQuery%5BregisterEndDate%5D=2020-01-01');
$csvResponse = $client->getResponse()->getContent();
return $csvResponse;
response is ...
ID,ステータス,分類,名前,名前(カナ),誕生日,郵便番号,住所,メールアドレス,電話番号,FAX,メモ,登録日,更新日\r\n
213,契約中,個人,"鶴田 秀夫","ツルタ ヒデオ",2000/07/24,1508207,神奈川県吉田市北区佐々木町小林7-4-5,hiroshi.nakatsugawa#yamaguchi.net,0310-282-609,0730-327-581,,"2010-01-25 00:00:
00","2010-01-25 00:00:00"\r\n
221,契約中,個人,"桑原 彩羅","クワハラ サイラ",2008/04/03,8103797,青森県杉山市西区石田町浜田3-4-10,vwakamatsu#kiriyama.jp,090-5710-4350,03849-5-5746,,"2010-01-09 00:00:00","2010-0
1-09 00:00:00"\r\n
237,契約中,個人,"堤 悟志","ツツミ サトシ",2001/04/29,6875750,栃木県佐々木市東区中島町浜田6-6-6,xtsuda#suzuki.com,022-557-4260,0573-01-2822,,"2010-02-07 00:00:00","2010-02-07 00:0
0:00"\r\n
273,契約中,個人,"富永 圭三","トミナガ ケイゾウ",2003/03/16,6314524,静岡県若松市東区廣川町青山10-9-6,yamaguchi.takuma#kondo.com,0020-062-493,06-3862-0779,,"2010-02-13 00:00:00","2
010-02-13 00:00:00"\r\n
.
.
.
What I want to do
I want to remove quotation from csv.
not
213,契約中,個人,"鶴田 秀夫","ツルタ ヒデオ",2000/07/24,1508207,
but
213,契約中,個人,鶴田 秀夫,ツルタ ヒデオ,2000/07/24,1508207,
※This personal information is fake.
What I did
str_replace(""", "", $csvResponse);
str_replace("\xEF\xBB\xBF", '', $csvResponse);
but, not working
The quotes serve a purpose here so you shouldn't be removing them haphazardly. The quotes prevent columns from breaking when they need to contain text that can also contain the delimiter. Like "Foo, bar, baz" for example. Removing the quotes turns this one column into 3 columns, which is obviously wrong.
Though, if you did want to remove them str_replace would certainly not be the way to go, because the quotes can be escaped to be literals inside the column. For example, using str_replace on this column: "He said \"this is crazy\", and left." would strip literals from the column value.
Instead you should load the CSV data with fgetcsv() or str_getcsv() and rebuild the CSV without the quotes like so...
foreach (str_getcsv($csvData) as $row) {
echo implode(",", $row), "\n";
}
This will give you back the literal values of each row without the quotes. Though any literal quotes inside those values will become quotes.
Related
I'm sorry that this is basic. When I use this PHP code it works fine:
$data = '{"reportID":1092480021}';
However, when I run my URL like this:
http://localhost:8000/new/reportget.php?type=1092480021
and use this PHP code:
$reportref = $_GET['type'];
$data = '{"reportID:".$reportref."}"';
I get the error
Error_description:reportID is required
I think it's an error with how I am joining my variable to the string but I can't understand where I am going wrong.
Your string is improperly quoted. To match the format in your first example use:
$data = '{"reportID":' . $reportref.'}';
Note there are no double quotes on the last curly.
Even better:
$reportref = 1092480021;
$data = [ 'reportId' => $reportref ];
var_dump(json_encode($data));
Output:
string(23) "{"reportId":1092480021}"
For simple view and understanding, can you try out:
$data = "{\"reportID\":$reportref}";
Think that should sort it out
Use it like this
data = '{"reportID:"'.$reportref.'"}"';
It isn't working because you wrap all the value within single quote and when it come to concatenate the $reprtref you put directly .$reportref without closing the first single quote and after putting the value to concatenate you forget to open another single quote
'{"reportID:".$reportref."}"';
the correct value is
'{"reportID:"' . $reportref . '"}"';
and to match the way you specify your $data value It must be like this
'{"reportID":' . $reportref . '}';
I am trying to write string to screen without quotes in php. I get the string data from a Mysql database and I try to write it to a textbox but double quotes are also written.
Example : "a2" .
i want that just write => a2 .
$gönder= (json_encode($gönder) );
echo $gönder;
string data is $gönder .
Use it like this :
echo trim($gönder, '"');
Since $gönder is a string, json_encode($gönder) will wrap it in double quotes.
Just echo it before applying json_encode:
$gönder = 'a2';
echo $gönder; // a2
$gönder = json_encode($gönder);
echo $gönder; // "a2"
use this code
echo trim($gönder, '"');
When using Abrahams/TwitterOAuth library, special HTML characters are placed in a Tweet.
$content = "test's";
$params = array(
"status" => $content
);
$result = $connection->post("statuses/update", $params);
results in
test's
within the posted Tweet status.
A vardump($content) in the browser reveals test's as expected.
I've tried html_entity_decode and htmlspecialchars_decode but neither work for the posted Tweet.
You need to tell it explicitly to convert the quotes. This is done with ENT_QUOTES.
Example:
echo htmlspecialchars_decode('test's',ENT_QUOTES) . "\n";
echo htmlspecialchars_decode('test's');
Output:
test's
test's
Demo: https://eval.in/426992
There are also other constants that can be used if you only want to decode single quotes etc. See the manual for more information, http://php.net/manual/en/function.htmlspecialchars-decode.php.
ENT_QUOTES Will convert both double and single quotes.
$content = "test's";
$content = htmlspecialchars_decode($content, ENT_QUOTES);
works for me.
Thanks chris85!
I am trying to read a file as a string and return it to the client to be executed as javascript code in order to save it as a variable. Like so
<?php
$fileName = 'target.js';
$codeAsString = file_get_contents($fileName);
$script = 'var code=\'' . $codeAsString . '\'';
echo $script;
?>
Once returned, I want the variable code to have a string representation of the contents of target.js. However, it isn't working. I suspect it has to do with new line characters and single/double quotes... In Python they have a multi line quote
"""This
is
a "multi"
line
'quote'
"""
Is there anything like this in Javascript/php? I can't even wrap my head around whether I need the single quotes around $codeAsString when appending it to $script. Do I have to manually go in and prepend backslashes before all quotes, double quotes, back slashes...Surely they have helper functions for this
thanks.
json_encode is your friend...
<?php
$fileName = 'target.js';
$codeAsString = file_get_contents($fileName);
$script = 'var code= '.json_encode($codeAsString) .';';
echo $script;
?>
Use PHP function json_encode.
I have a script to export data from MYSQL to a CSV file. the data is numbers, text and special chars. All fields are deliminated with double quotes and separated by commas.
I need to export data in the following form:
"this is a (x2") badly grammorize'd sentence. Yes, "no", maybe & more.","0043","false" etc..
However I can only get it to work when I apply htmlsepcialchars to each field. The data needs to remain as above, but when it comes into Excel or Calc some of the commas and single quotes etc screw it up. Meaning some of the sentence is in one cell and more in another.
$dataResult = mysql_query("SELECT * FROM data");
while ($rowData = mysql_fetch_row($dataResult))
{
for ($j=0;$j<32;$j++)
{
$csv_output .= '"'.htmlspecialchars($rowData[$j]).'",';
}
$csv_output .= "\n";
}
htmlspecialchars is only meant for escaping data inserted into HTML (as the function name suggest). For CSV data, consider writing a function adhering to the CSV standard, especially by using two quotes instead of one.
For example:
function csvspecialchars($msg) {
return str_replace('"', '""', $msg);
}
There may be other characters to escape as well. Check out the standard at RFC 4180.
According to wikipedia, you can escape a double quote by doubling it (eg, "this is a (x2"") badly grammorize'd sentence. Yes, ""no"", maybe & more.","0043","false"). That should also fix the problem with the commas and I can't say why single quotes would be a problem...
The built-in fputcsv function takes care of everything neccesary to output valid csv. Unfortunately it can only output to a file, but there's a workaround in the comments, credit goes to Guile:
<?php
function outputCSV($data) {
$outstream = fopen("php://output", 'w');
function __outputCSV(&$vals, $key, $filehandler) {
fputcsv($filehandler, $vals, ';', '"');
}
array_walk($data, '__outputCSV', $outstream);
fclose($outstream);
}
$mydata = array(
array('data11', 'data12', 'data13'),
array('data21', 'data22', 'data23'),
array('data31', 'data32', 'data23'));
outputCSV($mydata);
/* Output sent :
data11;data12;data13
data21;data22;data23
data31;data32;data23
*/