non-English language issue in creating php array from CSV file - php

In PHP, I am trying to create an array from a CSV file in this way.
// CSV file:
$csv = array_map('str_getcsv', file("{$uploadPath}/{$myFile}"));
Of course it is working for me, but on thing I have to fixed. If I have non-English language characters in my CSV file, then those characters not showing correctly in PHP. That mean its showing question marks instead of showing original characters.
This is the output of $csv array:
Array
(
[0] => Array
(
[0] => SISP-0002
[1] => Harpic Floral 500ml
[2] => ???????? ??????? 500ml
[3] => 4792037107765
)
)
UPDATE:
I have set charset inside page tag as shown here.
<meta charset="utf-8">
And also I have saved my CSV file with unicode(UTF-8)
Can anybody tell me what would be the possible workaround to fix this issue.

This appears to be the definitive question and answers for UTF-8 and php UTF-8 all the way through

Related

display data from an array in json form

i got an array from echo $posts
Array
(
[0] => Array
(
[id] => 14
[name] => اسطنبوليه
)
)
i have this array (part of the main array)and now i wish to convert it to json form. however i am not able to do so, i tried to convert the data through
echo json_encode ($posts);
but instead of original data i am getting an output u0627u0633u0637u0646u0628u0648u0644u064au0647n
can anyone tell how i can get the correct form
Please try echo json_encode($posts,JSON_UNESCAPED_UNICODE) (php 5.4 and above)
2 things. Firstly view source of the output in the browser and you should see it as a JSON encoded string rather than the interpreted version.
Secondly it looks like there is some character encoding issues as the string you're getting back is unicode. Make sure you have the encoding set right on your server and browser.

How to read data from this json format

I am stuck on this. This is a json_encoded string by the Wordpress plugin and saved into database.
I want to read it from my own database query. I am getting null when tried with var_dump .
It has some properties of code which is creating problem, I think.
Below is the data where from i want to read usable data for my use. I am using PHP and Mysql.
a:3:{i:0;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:3:"100";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:0;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}i:1;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:3:"200";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:0;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}i:2;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:8:"Infinite";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:1;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}}
I know this looks nasty but copy and try to decode it.
Ohh In Wordpress you can do as follows
$serialized = 'a:3:{i:0;s:5:"examp";i:1;s:6:"exampl";i:2;s:6:"examp2";}';
var_dump(unserialize($serialized));
Array
(
[0] => examp
[1] => exampl
[2] => examp2
)

cakephp sending encrypted data to library from Controller

While working with the cakephp, I found an issue mentioned below.
Fetched the encrypted field info from DB (encrypted using Security::rijndael)
Passed this whole data as an array format to the custom Library(Own created lib).
When i echoed the data in lib as well in controller I amazed to see the result. The value (encrypted one) are showing blank in the lib. Is I missed anything in codding? I searched on google but didn't get the satisfactory answer, Please help me out. Your help will really be appreciated.
Here is result i am getting in controller and Library respectively
Array
(
[0] => Array
(
[value] => s�i�(�RTf���cBЉF� | �r�n#ô�
)
)
Array
(
[0] => Array
(
[value] =>
)
)
Check your character encoding; a place I worked at ran into a similar issue and it was due to our db trying to encode characters it did not support. UTF-8 generic is a, well, generic encoding type.

json_encode problem

This has got me completely stumped:
print_r($json);
echo json_encode($json);
output:
Array
(
[query] => dia
[suggestions] => Array
(
[0] => Diana Johnson
[1] => Diane Abbott
)
)
{"query":"dia","suggestions":[null,null]}
What on earth is going wrong?
edit Just to add to the general wtf-ery of this, here's another sample:
Array
(
[query] => david
[suggestions] => Array
(
[0] => David Cameron
[1] => David Amess
[2] => David Anderson
[3] => David Blunkett
[4] => David Burrowes
)
)
{"query":"david","suggestions":["David Cameron",null,null,null,null]}
I'm posting this as an answer because I need the full formatting abilities of the normal answer box.
Yeah, it's UTF-8 all right. From the PHP interactive prompt:
php > $david = urldecode('David%A0Amess');
php > echo json_encode($david);
null
php > $david = urldecode('David%20Amess');
php > echo json_encode($david);
"David Amess"
php > $david = urldecode('David%c2%a0Amess');
php > echo json_encode($david);
"David\u00a0Amess"
So, we can assume that you're dealing with either ISO-8859 or Windows-1252, given that we're dealing with a broken NBSP. We can fix this with iconv:
php > $david = urldecode('David%A0Amess');
php > $david_converted = iconv('Windows-1252', 'UTF-8', $david);
php > echo json_encode($david_converted);
"David\u00a0Amess"
So, this means that you are going to need to not trust what you're pulling out of MySQL, assuming you've done the SET NAMES thing. Clearly something has gone awry when you were inserting data. You probably weren't giving MySQL well-formed UTF-8, and it stupidly did not complain. (If you were using other, smarter, more correct databases, and tried to insert the unencoded NBSP, they would have rejected the input.)
This looks like an autocomplete script. I assume your results are loaded from a database, are you sure they're utf-8? If you cannot replicate this functionality by hardcoding the array, then it's probably an encoding issue.
According to http://php.net/manual/en/function.json-encode.php, "This function only works with UTF-8 encoded data."
You can also use http://php.net/manual/en/function.json-last-error.php to see the last error.

Problems trying to explode a string

I am trying to split a text into an array using explode, but for some reason that does not work when the text is coming from a posted form.
If I run explode('|§|', 'qwe|§|asd|§|zxc'); I will get an array like:
Array
(
[0] => qwe
[1] => asd
[2] => zxc
)
BUT
If this input text comes from a form define like:
<form method="post">
Input: <input type="text" name="query" size="50" value="qwe|§|asd|§|zxc"><input type="submit" value="Parse">
</form>
I am getting the following array:
Array
(
[0] => qwe|§|asd|§|zxc
)
Im guessing this has to do with iso settings and that the text in the 'query' field has been altered in some way, but I can't understand how to fix. I have tried setting <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" /> and other charsets, but to no avail.
Any ideas? Thanks in advance.
Just an idea: The § sign is probably be converted to url format. Try urldecode() the string first.
I'm probably mistaken on this, but § may be a unicode character, which PHP does not yet support. Thus, there may be some issues when transferring from the form to the script.
Have you tried changing it to something more... normal? Like if you did qwe|~|asd|~|zxc instead, or maybe qwe|+~+|asd|+~+|zxc if you're concerned about what somebody would enter

Categories