0

I am fetching JSON data for use in my website and I need to echo it, like this:

<?php
    $json = file_get_contents("/lang/translator.php"); // uses preg_replace to remove whitespace/newlines from my actual json file and then echo's it
    $i18n = json_decode($json, true);

    if (htmlentities($_GET['lang'], ENT_QUOTES) == 'en')
    {
        $arr = 'i18n_en';
    }
    else if (htmlentities($_GET['lang'], ENT_QUOTES) == 'ru')
    {
        $arr = 'i18n_ru';
    }
?>

And I use it like this:

<?php echo $i18n[$arr]['string_key']; ?>

The string_key holds in its value either a English or a Russian translation of my site, depending in which JSON array it is.

The problem:

When I upload my JSON file which holds Cyrillic characters (Russian) this happens:

хорошо --> &#1093;&#1086;&#1088;&#1086;&#1096;&#1086;

Every single Cyrillic character gets converted to HTML entities. So I found out I could use html_entity_decode() to counter that but imagine how time-consuming it is to do this for every single <?php echo $i18n[$arr]['string_key']; ?> call I have in my code. Isn't there any way around that? I tried passing $i18n to html_entity_decode() but it expects a string, not an array of strings. Any ideas?

My JSON example:

{
    "i18n_en":
    {
        "key0": "value0",
        "key1": "value1"
    },
    "i18n_ru":
    {
        "key0": "value0",
        "key1": "value1"
    }
}
2
  • Why are you calling htmlentities on the $_GET values? htmlentities should only be used when displaying a value in a page and you don't want it to be rendered as HTML. Commented Apr 30, 2016 at 22:19
  • I just find this a good practice to defend against hacker attacks... Maybe I'll remove the calls. Commented Apr 30, 2016 at 22:23

3 Answers 3

6

If you want to run html_entity_decode() on an Array of strings you can use array_map. Like this:

$resultArray = array_map("html_entity_decode", $inputArray);

Sign up to request clarification or add additional context in comments.

2 Comments

I get this: Warning: html_entity_decode() expects parameter 1 to be string, array given...
It's not html_entities_decode but html_entity_decode.
3

Once you have set $arr, you could apply html_entities_decode() to every string in the specified language with a foreach loop.

Something like this:

if (htmlentities($_GET['lang'], ENT_QUOTES) == 'en')
{
    $arr = 'i18n_en';
}
else if (htmlentities($_GET['lang'], ENT_QUOTES) == 'ru')
{
    $arr = 'i18n_ru';
}

foreach ($i18n[$arr] as &$myString) {
    $myString = html_entity_decode($myString);
}

Edit. Two posible solutions to fix Warning: Illegal string offset 'i18n_en'.

1:

foreach ($i18n[$arr] as &$myString) {
    if(isset($myString)){
        $myString = html_entity_decode($myString);
    }
}
foreach ($i18n[$arr] as &$myString) {
    $myString = html_entity_decode($myString, ENT_COMPAT, 'UTF-8');
}

Or maybe, both combined. Let me know if this works please.

6 Comments

This works but how to echo the new string? I tried doing echo $myString[$arr]['key']; but I get Warning: Illegal string offset 'i18n_en'.
Hmm... is maybe some of those strings empty?
Yes, about 2~3 of the Russian strings are empty.
Ok, I have two ideas for that warning, let me put it in the answer to make it simple to read. Johny P.
If I try to call it like $myString[$arr]['slang'], it doesn't. And I don't know any other way of calling it. If I just call echo $myString; I get a totally random string from the JSON array.
|
0

You can use PHP's array_map() function to call a function over each item in an array. An example can be seen below.

$decodedArray = array_map("decode",$i18n);

function decode($toDecode) {
    return html_entity_decode($toDecode);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.