Format buffer, php

Asked

Viewed 67 times

1

I don’t know if the right word is buffer but how do you make Javascript code "clean" that way:

({"content":"<div id=\"_sub-item\">Espere, ser\u00e1 exibido ap\u00f3s o carregamento dos itens.<\/div>"

You see that it anchored all " and encoded ready to display to the browser?

How do you do this with PHP?

  • utf8_encode() and utf8_decode() tried that?

  • @rray does not return as expected

  • That’s a sure json?

  • @rray I believe that yes, it has to have that same effect?

  • Accept any answer if you solved the problem.

3 answers

2

The encoding returned in your example is part of the serialization generated by the function json_encode.

Example:

json_encode(array(
 'conteúdo' => '<div class="alguma-coisa">Alguma coisa e acentuação para testar</div>'
));

If you want to encode some string with escaped characters for HTML entities, you can use the function htmlentities.

echo htmlentities('"meu nome é wallace"');

1

Utilize json_encode() to have the expected exit:

<?php
$str = "<div>Espere, será exibido após o carregamento dos itens.</div>";
$json = json_encode($str);
print_r($json);

Exit:

"<div>Espere, ser\u00e1 exibido ap\u00f3s o carregamento dos itens.<\/div>"

1

This apparently seemed to me to be a JSON output, in PHP you convert this output like this in PHP:

    $saida  = array(
                'content'=>'<div id="_sub-item">
                               Espere, será exibido após 
                               o carregamento dos itens.
                              </div>'
              );
   echo json_encode($saida);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.