PHP database text for html

Asked

Viewed 795 times

6

I’m having trouble returning text with HTML formatting to the view. I need to return it with break lines. I also tried to put it without the tags HTML in the database and then used the function nl2br() transforming /n in tag <br>, but in both cases it returns with "" quotation marks between the text as it is in the image below the firebug. I tried with different PHP functions to remove the quotation marks, but without success.

inserir a descrição da imagem aqui

3 answers

7


If you want to take out those quotes then you’ll have to do so:

 trim(nl2br($texto));

Unless at the time you are printing the variable you have done so:

<div>
   <?php echo  nl2br($texto); ?>
</div>

Because in this case, the line break given in the div counts as space (which generates those quotes in the firebug).

In case, you can do so:

<div><?php echo trim(nl2br($texto)) ?></div>

I have made the following test for you to verify that my last two examples have differences:

<?php ob_start() ?>
<div>Meu nome é Wallace</div>
<?php var_dump(ob_get_clean()) ?>

<?php ob_start() ?>
<div>
    Meu nome é Wallace
</div>
<?php var_dump(ob_get_clean()) ?>

The results are:

string(31) "<div>Meu nome é Wallace</div>
"

string(34) "<div>
    Meu nome é Wallace
</div>
"

Note that these line breaks are processed as string content.

The first string had the quotation mark down because I had to break the line to put the var_dump(ob_get_clean())

  • I thought about it too, but I was wondering if it was the right way hehe

  • +1 ta light eh hsausha

  • I also use this form because it wouldn’t be right? @Diegofelipe

  • I will make the answer more complete. Wait for the prints

  • 2

    @Bia not that it was not the correct form, out of me to affirm it. I was just wondering if it was the best way, but from Wallace’s explanations, it’s more than proven :)

  • 1

    It worked, thank you.

Show 1 more comment

2

If these double quotes are not visible in your html the explanation of them appears in your inspector element is simple: Text organization.

As quoted in this reply : SOEN

Original

This is just the way that Chrome presents the text element inspector. You can see white-space Better this way. The Quotes are Purely virtual.

Translation

This is just the way Chrome presents the contents of the node text in Element Inspector. You can view it better like this. Citations are purely virtual.

If there are quotes in your html use Trim to remove them:

Example:

$str = '"Hello"';
echo trim($str, '"'); // saída: Hello
  • We are in this case assuming that the problem is the saved rooms in the bank. But I still don’t think that’s it. The quotes are not from the words, but from the interpreter of firebug

  • 1

    @Wallacemaxters was what I thought, I’m used to using only Firebug in Mozilla, I used Inspect Chrome Element and my texts also have Double Quotes, it seems that it is the same program, if the quotes do not appear in the view she should not worry.

1

I believe the problem is not with the function nl2br. Check the output before applying the function to be sure. It also seems that the firebug automatically quotes the console when it comes to more than one line, to improve the view.

  • +1 I just don’t understand why of so many negatives, if you see in this SOEN response you realize that the element inspector acts like this: http://stackoverflow.com/questions/12608899/chrome-adding-quotes-to-html

  • Hi friend, thanks for the reference. It’s not about negatives regarding firebug. The question is to identify whether the situation presented is a "problem" (answer with quotes from PHP), and solve it, or just a firebug behavior (unquoted PHP response, quoted in the console).

Browser other questions tagged

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