-1
<div class="as space divider"></div>
<span class="as medium text"> <?php echo Validator::sanitize($this->row->body, "string", 300)?> 
</span>
<a href="#jump_desc">(read more)</a>

Hi, please help... I have a .php with this code, but line breaks they are not considered.

Who can please tell me how I can do?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Higo Kat
  • 7
  • 1
  • Do you mean that the string in `$this->row->body` contains newlines that are not respected when seen in the HTML Page? – RiggsFolly Aug 21 '20 at 09:25
  • if it's a `\n` character, you could prolly try your luck using `nl2br()` – Kevin Aug 21 '20 at 09:27
  • That's right, the text is duplicated without respecting line breaks that instead they are present in the long description text. – Higo Kat Aug 21 '20 at 09:31
  • Thanks for the answer, I'm a beginner and I don't really know how to do it. – Higo Kat Aug 21 '20 at 09:38
  • `nr2br(Validator::sanitize($this->row->body, "string", 300))` – RiggsFolly Aug 21 '20 at 09:41
  • _“I'm a beginner and I don't really know how to do it”_ - that should not stop you from typing something like “php preserve line breaks” or similar into Google on your own though, right? And that would have found https://stackoverflow.com/questions/5048849/preserve-line-breaks-from-textarea-when-writing-to-mysql and many others in no time. – CBroe Aug 21 '20 at 09:47
  • Thank you for the advice you wanted to give me, please consider that I have already done on my own initiative what you have proposed, but I have not found an effective solution for the type of code I have posted. – Higo Kat Aug 21 '20 at 10:16

1 Answers1

0

If your body contains newlines, you can do this:

<span class="as medium text">
    <?= nl2br(Validator::sanitize($this->row->body, "string", 300))?> 
</span>

The magic is "nl2br" which means "new line to break row".

Read more: nl2br function

PatricNox
  • 3,306
  • 1
  • 17
  • 25
  • Thanks for replyr, this solution you have proposed seems not to be ideal, the
    are not considered.
    – Higo Kat Aug 21 '20 at 10:05
  • so your string consists of
    ? Then what happends if you use htmlentities instead of nl2br?
    – PatricNox Aug 21 '20 at 11:21
  • my string is a short description taken from row->body. Text content is entered in a text editor with
    . Short version is shown with no line breaks. tried with this code but it doesn't solve the problem... ` = nl2br(Validator::sanitize($this->row->body, "string", 300))?> `
    – Higo Kat Aug 21 '20 at 12:25
  • when you print it, you mean you see the
    tag in site? I think its your validator::sanitize that escapes it from being intepreted as real html
    – PatricNox Aug 21 '20 at 12:32
  • @ PatricNox No, I dont see
    tag in site, the new sentence that should be shown on a new line of text remains in the same line after
    . I don't know what to do to fix it.
    – Higo Kat Aug 21 '20 at 12:48