turkish/hungrairan/ polish letters not executing properly - php

I'm a bit of a novice here so my apologies if I didnt derive this answer from earlier posts I read.
I put together a file in php. Everything works when the URL to the php file is executed, except some of the Polish and Turkish characters come up as question marks (in utf8 and unicode) and simply disappear and turn into latin letters in anicode.
i edited both in wordpad and notepad.
How can I fix this problem, please?
thanks.
function array_utf8_encode($dat)
{
if (is_string($dat))
return utf8_encode($dat);
if (!is_array($dat))
return $dat;
$ret = array();
foreach ($dat as $i => $d)
$ret[$i] = array_utf8_encode($d);
return $ret;
}
header('Content-Type: application/json');
// Return the array back to Qualtrics
print json_encode(array_utf8_encode($returnarray));
?>

Sounds like you might need to add the json_encode JSON_UNESCAPED_UNICODE option:
print json_encode(array_utf8_encode($returnarray), JSON_UNESCAPED_UNICODE);
Other json_encode options are at: http://php.net/manual/en/json.constants.php

Related

Emoji name "family_mothers_one_boy" or "woman-woman-boy"?

I have a reference emojis file used by my php code. Inside there is for example "woman-woman-boy", but the browser (chrome) replaces this name by "family_mothers_one_boy"...
Why are there two versions of emojis' names?
Is there en (some) error(s) in my file, or should I have to do something in my code to avoid the conversion?
NOTE:
The code related to this emoji is:
1F469;‍👩‍&#x1F466
Here are the two functions I'm using to manage the emojis:
1. When I display the emoji, I replace the tage :name: by the HTML rendering (using unicode)
function replaceEmojiNameByUnicode($inputText){
$emoji_unicode = getTabEmojiUnicode();
preg_match_all("/:([a-zA-Z0-9'_+-]+):/", $inputText, $emojis);
foreach ($emojis[1] as $emojiname) {
if (isset($emoji_unicode[$emojiname])) {
$inputText = str_replace(":".$emojiname.":", "&#x".$emoji_unicode[$emojiname].";", $inputText);
}
else {
$inputText = str_replace(":".$emojiname.":", "(:".$emojiname.":)", $inputText);
}
}
return $inputText;
}
2. When I want to propose the list of emoji I display an HTML SELECT in the page. Teh following function return the list of option to add inside:
/* Display the options in the HTML select */
function displayEmojisOptions(){
$emoji_unicode = getTabEmojiUnicode();
foreach ($emoji_unicode as $name => $unicode) {
echo '<option value="&#x'.$unicode.';">'.$name.' => &#x'.$unicode.';</option>';
}
}
In the array $emoji_unicode there is one entry (with 3 semi-column removed to not display emoji here):
'family_one_girl' => '1F468;&#x200D&#x1F469&#x200D&#x1F467',
For example: In order to make it works, I have to replace the line 'thinking_face' => '1F914', by 'thinking' => '1F914',
My question is: why ??
Thank you
Nop, the emoji text was changed by no code... I guess it was due to a wrong emoji file I used... I correct all the emoji manually and now I did not see the mismatch anymore...
If someone need the corrected file, I can provide it.

special characters french accents

We are fetching data from csv file through php, and trying to compare data to insert the right data in our database but php comparison is not working as data in file is with french accents.
Here is a piece of code we are working with.
if($data[0]=='Expression' && $data[1]=='Domaine (Domain)' && utf8_decode($data[2])==utf8_decode('DŽfinition (Definition)') && $data[3]=='Commentaire (Commentary)' && $data[4]=='Voir aussiÉ (See also É)' && $data[5]=='ƒquivalent anglais (English equivalent)' && $data[6]=='En contexte / exemple(s) É (In context / examples)' && $data[7]=='Source' )
{
echo "<tr>
<td>".$data['0']."</td>
<td>".$data['1']."</td>
<td>".$data['2']."</td>
<td>".$data['3']."</td>
<td>".$data['4']."</td>
<td>".$data['5']."</td>
<td>".$data['6']."</td>
<td>".$data['7']."</td>
<td><i class='fa fa-close text-navy'></i></td>
</tr>";
return true;
}
else
{
echo "invalid data";
exit;
}
We have tried with this as well.
function convert($data)
{
$value = utf8_encode($data);
$value = iconv('UTF-8', 'ASCII//TRANSLIT', $value);
return $value;
}
Header is already placed output is fine
header('Content-Type: text/html; charset=iso-8859-1');
We have tried with several php functions like utf_decode,html entities, html special char, htmlspecialchar_decode but nothing is working.
echo print_r(utf8_decode($data[2]));
output is as following:
D?finition (Definition)1invalid data
Actual word is : DŽfinition (Definition)'
We are working on french dictionary and need to do real time searching on the data as well, please help with mysql as well, like which functions are needed to be called before insertion for decoding and which functions are needed to be called before showing data back to user encoding!
Hope my question is bit clear.
Thanks in advance
To save the CSV in UTF8, open it in notepad.
And Go to File - Save As
Change the Encoding to UTF-8.
or with Libre Office :
https://csvimproved.com/support/questions-and-answers/916-save-a-csv-file-as-utf-8
Hope it ll help

PHP json encode - Malformed UTF-8 characters, possibly incorrectly encoded [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 7 months ago.
I'm using json_encode($data) to an data array and there's a field contains Russian characters.
I used this mb_detect_encoding() to display what encoding it is for that field and it displays UTF-8.
I think the json encode failed due to some bad characters in it like "ра▒". I tried alot of things utf8_encode on the data and it will by pass that error but then the data doesn't look correct anymore.
What can be done with this issue?
The issue happens if there are some non-utf8 characters inside even though most of them are utf8 chars. This will remove any non-utf8 characters and now it works.
$data['name'] = mb_convert_encoding($data['name'], 'UTF-8', 'UTF-8');
If you have a multidimensional array to encode in JSON format then you can use below function:
If JSON_ERROR_UTF8 occurred :
$encoded = json_encode( utf8ize( $responseForJS ) );
Below function is used to encode Array data recursively
/* Use it for json_encode some corrupt UTF-8 chars
* useful for = malformed utf-8 characters possibly incorrectly encoded by json_encode
*/
function utf8ize( $mixed ) {
if (is_array($mixed)) {
foreach ($mixed as $key => $value) {
$mixed[$key] = utf8ize($value);
}
} elseif (is_string($mixed)) {
return mb_convert_encoding($mixed, "UTF-8", "UTF-8");
}
return $mixed;
}
Please, make sure to initiate your Pdo object with the charset iso as utf8.
This should fix this problem avoiding any re-utf8izing dance.
$pdo = new PDO("mysql:host=localhost;dbname=mybase;charset=utf8", 'user', 'password');
With php 7.2, two options allow to manage invalid UTF-8 direcly in json_encode :
https://www.php.net/manual/en/function.json-encode
json_encode($text, JSON_INVALID_UTF8_IGNORE);
Or
json_encode($text, JSON_INVALID_UTF8_SUBSTITUTE);
you just add in your pdo connection charset=utf8
like below line of pdo connection:
$pdo = new PDO("mysql:host=localhost;dbname=mybase;charset=utf8", 'user', 'password');
hope this will help you
Remove HTML entities before JSON encoding. I used html_entity_decode() in PHP and the problem was solved
$json = html_entity_decode($source);
$data = json_decode($json,true);
Do you by any chance have UUIDs in your result set? In that case the following database flag will help:
PDO::DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER => true
If your data is well encoded in the database for example, make sure to use the mb_ * functions for string handling, before json_encode. Functions like substr or strlen do not work well with utf8mb4 and can cut your text and leave a malformed UTF8
I know this is kind of an old topic, but for me it was what I needed. I just needed to modify the answer 'jayashan perera'.
//...code
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
for ($i=0; $i < sizeof($result) ; $i++) {
$tempCnpj = $result[$i]['CNPJ'];
$tempFornecedor = json_encode(html_entity_decode($result[$i]['Nome_fornecedor']),true) ;
$tempData = $result[$i]['efetivado_data'];
$tempNota = $result[$i]['valor_nota'];
$arrResposta[$i] = ["Status"=>"true", "Cnpj"=>"$tempCnpj", "Fornecedor"=>$tempFornecedor, "Data"=>"$tempData", "Nota"=>"$tempNota" ];
}
echo json_encode($arrResposta);
And no .js i have use
obj = JSON.parse(msg);

php strstr not detecting substring in a arabic string

Can anyone please tell me ,How to make string matching function work with Arabic keywords?
The Following keywords are detected as Unmatched in php. Though these are matching in normal browser searching.
if (strstr('الهيئة العامة للقوى العاملة', 'قوى العامله') != false) {
echo'in';
} else {
echo'out';
}die;
Result comes as 'out'.
Thanks
Try this .. I have PHP Version 5.6.23
<?php
$string = 'الهيئة العامة للقوى العاملة';
$find_me = 'قوى العاملة';
if (strstr($string, $find_me) != false) {
echo'in';
} else {
echo'out';
}die;
?>
You can see output here https://eval.in/764085
The result for the match you are doing should be out, so it's working just fine for you. There is no match for the search you are doing since the last letter in the word العاملة is different between the two strings.
Try to re-save the page in
UTF-8 Wihtout Bom
this work for me

utf-8 problem in using jquery autocomplete tags

hey mates . recently i used jquery auto-complete tag
http://devthought.com/projects/jquery/textboxlist/
everything goes fine except utf-8 tag suggesting , only English tags are suggested
i guess something goes wrong with script lines
it works fine with English tags but not with multi byte languages like Persian
Probably line 212 in the TextboxList.Autocomplete.js is to blame:
regexp = new RegExp('\\b' + escapeRegExp(search), insensitive ? 'i' : '');
That's looking for the given character after a word boundary. But word boundaries are dependent on recognition of word characters, and JavaScript RegExp's list of word characters is just the ASCII alphanumerics plus _. Because RegExp knows nothing about Unicode this won't work where the word begins with a non-ASCII character.
You could try getting rid of the \\b in which case it would match any suggestion with the given string anywhere inside it, not just at the start of words.
Your HTTP header is wrong. It should be:
header('Content-Type: application/json; charset=UTF-8');
You can also shorten your code and do the sorting with MySQL:
$sql = 'SELECT `tag` FROM `'.$prefix.'_tags` ORDER BY `tag`';
$result = $db->sql_query($sql);
if (!$result) {
header($_SERVER['SERVER_PROTOCOL'].' 500 Internal Server Error');
echo mysql_error();
exit;
}
$response = array();
$i = 0;
while ($row = $db->sql_fetchrow($result)) {
$response[] = array($i++, $row);
}
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($response);
Your content-type header is somewhat wrong. First, it should be content-type:something; charset=something, that is, content-type:text/html; charset=utf-8.
But it is actually suggested to use content-type application/json, see here What is the correct JSON content type?
So, you could do it like this
header("Content-Type:application/json; charset=UTF-8");
Probably line 215 in the TextboxList.Autocomplete.js is to blame:
if (regexp.test(values[i][1])) newvals.push(values[i]);
covert it to
if (values[i][1].indexOf(escapeRegExp(search)) != -1) newvals.push(values[i]);
Bobince (first answer) right, the problem is in line 212, but resolved it can be in line 215

Categories