Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

180
Visualizações
Trying to get text with properties from html data

I'm trying to get text from HTML with it's properties (bold, underlined, italic, superscript etc.) but I am struggling with nested ones (like <b> Lorem <u> Ipsum </u></b>, in this case Lorem should be bold and Ipsum should be bold and underlined).

Example Data

<p> Normal<b>Bold</b> <b>Bold<u>Underlined</u></b> <b><i>Bold Italic</i></b><p/>

I need to use this texts in Indesign Script and I need to assign character styles for these properties. Is there any tool or technique for PHP or Javascript that I can use?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try if DOMParser is available in the environment where you're going to run your JS code.

This parses the html string and outputs a tree structure of the nodes and their texts.

const htmlString = '<p> Normal<b>Bold</b> <b>Bold<u>Underlined</u></b> <b><i>Bold Italic</i></b><p/>';

const htmlElement = (new DOMParser().parseFromString(htmlString, 'text/html')).firstChild.childNodes[1].firstChild;

const tree = convertDomToArray(htmlElement);

console.log(tree);


function convertDomToArray(element) {
  if (element.nodeName === '#text') {
    return element.nodeValue;
  }
  
  let children = [];
  for (let childElement of element.childNodes) {
    children.push(convertDomToArray(childElement));
  }
  
  let output = {};
  output[element.nodeName] = children;
  
  return output;
}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda