PHP json decode with æøå - php

I have a problem with some json decode of my products, which have special characters "æøå".
Decode code + echo:
$products = json_decode($details['items'],true);
foreach($products as $pro){
..
<?php echo $pro['name']; ?>
..
In my database the name of the product looks like this: 'SpÃ¥ner'. However, in the echo it's: 'Spu00e5ner'. It need to be 'Spåner'.
I know the code isn't updated, but there gotta be a way to show the special characters.

I made a function that might help you with your problem.
function convertChars($char){
$return = html_entity_decode(htmlentities($char, ENT_QUOTES, 'UTF-8'), ENT_QUOTES , 'ISO-8859-15');
$return = iconv("UTF-8","ASCII//TRANSLIT",$return);
return strtolower(preg_replace('/[^a-zA-Z0-9]+/','',$return));
}

Related

How to JSON encode an array with French accents?

There are many threads about this but none of them helped me solve this problem.
$array=array(
"dépendre"=>"to depend",
"dire"=>"to say",
"distraire"=>"distracted",
"être"=>"to be (being)",
);
Gets encoded like this with json_encode :
"d\u00e9pendre":"to depend","dire":"to say","distraire":"distracted","\u00eatre":"to be (being)"
So far I have tried this:
array_walk_recursive($array,function($value,$key) {
$key = urlencode(utf8_decode($key));
});
Try this,
json_encode($array, JSON_UNESCAPED_UNICODE);
You should get this result;
{
"dépendre":"to depend",
"dire":"to say",
"distraire":"distracted",
"être":"to be (being)"
}
Please check the result from the following code:
<?php
$x=array(
"dépendre"=>"to depend",
"dire"=>"to say",
"distraire"=>"distracted",
"être"=>"to be (being)",
);
$encoded = json_encode($x);
var_dump($x);
var_dump(json_decode($encoded, true));
The string you get in your question is a correctly escaped JSON and could be successfully decoded.

turkish/hungrairan/ polish letters not executing properly

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

PHP file_get_contents and domxpath UTF-8 encoding issue

I'm reading an external file which contains this :
<td>ÖZGÜR </td>
And I read it like this :
$html = file_get_contents("");
$html = str_replace("charset=iso8859-9" , "charset=utf-8" , $html);
$rows = $x->query('//tr[contains(#class,"tablerow")]');
foreach($rows as $node)
{
echo $node->childNodes->item(12)->nodeValue;
}
it does not echo ÖZGÜR , but it echoes �ZGÜR.
what type of encoding function should I call here ?
Thanks for any help !
you should use
mb_internal_encoding("UTF-8");
function to change the encoding instead of
$html = str_replace("charset=iso8859-9" , "charset=utf-8" , $html);
if data is stored in database than you need to change the connection encoding at the time of data fetching.
mysql_set_charset('utf8',$constring) than you will be able to retrieve in the UTF-8 format
Try converting $html to utf8 after you set it with file_get_contents, something like
$html = iconv('ISO-8859-9', 'UTF-8', $html);

Get values from URL using GET from encode UTF-8

let me start by saying that I have no idea how to formulate this question, I have spend the last two days looking for some ways to to the following.
I send some information encoded using base64 as follow....
Values are:
Lóms Gruñes
this values came from an input box
beacuse of that I do this
$name = htmlentities(( $_POST ['name'] ) , ENT_NOQUOTES, 'UTF-8');
$midn = htmlentities(( $_POST ['midn'] ) , ENT_NOQUOTES, 'UTF-8');
the output for this should be
Lóms Gruñes
And that is what gets encode, until that everything is fine, and I can do whatever I need with that, in this case I'm going to encode it using base64
$datas ='&name='. $name .'&midn='. $midn;
$bd = base64_encode($datas);
// Now lets send that info to another file..
header( 'Location: other.php?d=$db' ) ;
So the url will be something like
domain.com/other.php?d=TCZvYWN1dGU7bXMgR3J1Jm50aWxkZTtlcw==
So now lets decode it so that it can be saved...
$ds = base64_decode($_GET['d']);
parse_str($ds, $params);
$name = htmlentities($params['name'], ENT_NOQUOTES);
$midn = htmlentities($params['midn'], ENT_NOQUOTES);
It looks pretty straight forward isn't it... but here is the problem because when I try to use the values nothing happen... lets say I just want to echo it...
echo $name . '<br>';
echo $midn;
What I get is
L
Gru
so where is the ó and the ñ?
ok, let say I don't encode anything so the URL will look like this...
domain.com/other.php?name=Lóms&midn=Gruñes
// and the I use echo like this:
echo $_GET['name'] . '<br>';
echo $_GET['midn'];
// the output is
L
Gru
Even if I put : header ('Content-type: text/html; charset=utf-8'); after the <?php ... nothing happen... so, the question... how can I get the ó as a value or better yet, how can I send the í,ó,ñ,á...etc as is in the url domain.com/file.php?data=íÄÑó and retrive it as is and save it as is and display it as is...
I;m not sure if this has some relevant information, the data is going to be saved in a DB, the DB is InnoDB, utf8_general_ci
Thank you for taking the time...
Always escape/encode for the technology you're using.
To get the UTF-8 encoded values from the form submission:
$name = $_POST['name'];
$midn = $_POST['midn'];
To output those into HTML:
<p><?php echo htmlspecialchars($name, ENT_COMPAT, 'UTF-8'); ?></p>
To embed them in a URL:
$url = sprintf('foo.php?name=%s&midn=%s', rawurlencode($name), rawurlencode($midn));
// or
$url = 'foo.php?' . http_build_query(array('name' => $name, 'midn' => $midn));
In foo.php, to get the values back:
$name = $_GET['name'];
$midn = $_GET['midn'];
And again, to output those into HTML:
<p><?php echo htmlspecialchars($name, ENT_COMPAT, 'UTF-8'); ?></p>
And that's basically all you need to do. Always escape values using the right function for the medium, don't escape more or earlier than you need to.
First of all, if you want to decode htmlentities try html_entity_decode().
For encoding the url try urlencode().
Example from php.net:
<?php
$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '<a href="mycgi?' . htmlentities($query_string) . '">';
?>
Hope it helps!
May be this will work for you . Instead of using htmlentites I used rawurlencode and rawurldecode function .
Below is same code
First get the values from the
$name = rawurlencode('Lóms Gruñes'); use can use //( $_POST ['name']
$mid = rawurlencode('Gruñes'); and here ($_POST ['midn'] )
then
echo $datas ='name='. $name .'midn='. $midn;
$en = base64_encode($datas);
Append this on next url . Then get the value and decode it using base64_decode function
$dd = base64_decode($en);
parse_str($dd);
echo $name;
echo $midn;

Encoding problems with exec

I'm fetching some info from SVN on an internal webpage for our company using exec and SVN log commands. The webpage is ISO-8859-1 by requirement, but the SVN-server/exec outpu is UTF8 and special characters are decimal encoded not the "normal" encoding.
This makes it impossible to use UTF8_decode or similar function as far as I've been able to tell, and I can't really get the grips on exactly how the return is formatted otherwise str_replace would have worked as an workaround for the moment. For instance as far as i can see ä is represented by ?\195?\164, but I cant find and replace that string in the output so probably there are some other things going on that I'm missing
My SVN-server is a CentOS and the webserver is Debian running Apache if the culprit can be there somwhere
Pseudocode
exec('svn log PATH' , $output);
foreach ($output as data){
$data = str_replace(array('?\195?\165', '?\195?\182'), array('å','ö'), $data);
echo $data . '<br>';
}
foreach ($output as data){
$data = utf8_decode($data);
echo $data . '<br>';
}
foreach ($output as data){
$data = mb_convert_encoding($data, 'ISO-8859-1', 'UTF-8');
echo $data . '<br>';
}
Example string echoed is "Buggfix f?\195?\182r 7.1.34" but should be "Buggfix för 7.1.34"

Categories