How to avoid excessive line breaks in a textarea

Asked

Viewed 213 times

3

So guys, how can I replace in a text, excessive line breaks, making when I have more than one line break in a row these occurrences are replaced by a single one, regardless of how many were placed. For example:

The text typed by the user:

<textarea>
    Paragráfo de texto
    <br />
    <br />
    Paragráfo de texto
    <br />
    Paragráfo de texto
    <br />
    <br />
    <br />
    Paragráfo de texto
</textarea>

The text to be presented:

<textarea>
    Paragráfo de texto
    <br />
    Paragráfo de texto
    <br />
    Paragráfo de texto
    <br />
    Paragráfo de texto
</textarea>

I need it in php :)

  • See if this OS link helps http://stackoverflow.com/questions/10757671/how-to-remove-line-breaks-no-characters-from-the-string

2 answers

3


  • Helped Kenny a lot! Vlw :)

  • The orders boss! ;)

1

The question is vague, but it might solve:

preg_replace("/[\r\n]+/", "\n", $string_recebida_do_textarea);

Example of how it works:

//Simulando uma string com várias quebras de linha
$str = '
a


b





c
';

// Aqui o resultado sanitizado
// A tag <pre> é desnecessária. Foi colocada para que possa ver pelo browser como se fosse plain/text 
echo '<pre>'.preg_replace("/[\r\n]+/", "\n", $str).'</pre>';
  • didn’t work for me :(

  • It’s hard to understand with a vague explanation and vague feedback yet.

  • Putz guy, all right. I’ll try to improve a little so you understand better, although I don’t agree with your definition of vague.

  • Daniel, I may agree that my comment may be vague, but the question is not so much. I think the way you approach this is incorrect. That’s my opinion. I believe that instead of you saying that "it’s vacant" you could have asked the questions you asked in the previous comment. And I say this not for myself, but for new users. Can you imagine approaching a new user here in the OS like this? Maybe the guy won’t even come back. Anyway, that’s my opinion and that’s it. And yes, I understand what preg_replace does in the example you sent, but in my case there is a peculiarity that I discovered

  • The results are being saved in the bank, and it seemed to me that there was some modification that caused me strangeness. The line breaks were coming with a <br />, followed by an n that was the moment I was able to replace. I’ll check here and then I’ll tell you how it turned out. Thanks for trying to help

Browser other questions tagged

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