Convert unicode back to text - Wordpress JSON api

Viewed 2465

Working on a Wordpress feed on an external site and have encountered an issue with post titles that contain characters such as single quotes and dashes.

JSON output looks like this:

{"rendered":"Some title post text here – some text after dash"}

{"rendered":"‘Title text in quotes ’: More title text"}

What's the best way to convert these unicode characters back to text on the front end? At the moment it's displaying the unicode.

Thinking there must be a javascript solution. Ideally the solution would cover all bases.

4 Answers

If you are working with VUEjs you can use the v-html directive

There's an NPM package that performs this and it's well documented. html-to-text

const { convert } = require('html-to-text');

const text = convert('<div>Nope Its not Ashton Kutcher. It is Kevin Malone. <p>Equally Smart and equally handsome</p></div>', {
    wordwrap: 130
});
console.log(text); 
// Results:
// Nope Its not Ashton Kutcher. It is Kevin Malone.

// Equally Smart and equally handsome

Assuming you're using a front end framework like React:

import { convert } from 'html-to-text';

export function App(props){

return (

  <div>
    <h1>{convert(props.wpData, {wordwrap: null})}</h1>
  </div>

    )}
Related