[✅ Solution] HTML Encoding / Decoding // WordPress Project

Hi there,
I’m continue working with WordPress. I get my titles retrieved via API like this:

„go-digital“ oder „Digital Jetzt!“? – 2023

A way would be to use he.decode with the he library. Probably its not available?
Is there another way than manually replacing all those encoded characters?

Can you use:

decodeURIComponent(„go-digital“ oder „Digital Jetzt!“? – 2023);

Sorry on my phone at the moment but hopefully that will help you

1 Like

Thanks for the idea, but its not working. decodeURIComponent is handling URI encoding. Not HTML entity encoding.

Give this a try

function decodeHtml(html) {
    var txt = document.createElement("textarea");
    txt.innerHTML = html;
    return txt.value;
}

let encodedString = "„go-digital“ oder „Digital Jetzt!“? – 2023";
let decodedString = decodeHtml(encodedString);

decodedString;
2 Likes

I ran some tests and I don’t think document is available, Loadash also didn’t seem to work however a slight modification to John’s code did appear to work:

function decodeHtml(html) {
    return html.replace(/&#(\d+);/g, function(match, dec) {
        return String.fromCharCode(dec);
    });
}

let encodedString = base;
let r4 = decodeHtml(encodedString);

console.info('R4', r4);
5 Likes

Thanks Jason, that solved it! Thank you also @1F2Ns for contributing to a solution!

3 Likes

I love this community and the level of teamwork that exists -reminds me of Rainer and Andreas when GF first launched. I hope we never lose this collaborative effort.

6 Likes