How to remove this line break?

Asked

Viewed 9,847 times

0

I have a page that uses a text editor of type 'wysiwyg' which in the case is a textarea element, this generates an html code for text that the user type, I save the value of this field in onblur of the same through an ajax that makes save in SESSION via php, the problem is later, I want to make this text that the user typed appear on the screen (even if the user updates the page), and I have trouble setting the text in the text editor, because the generated code is generated with line break, and by putting this value in javascript gives error due to this line break already looked for ways to remove these line breaks, but n found one that worked.

function that calls ajax:

function SalvaSession(valor){
      $.post( "ajax_salva_session.php",
            {
                'texto' : valor  //valor recebe o html do editor de texto
           },
           function (data) {
           }
      );
 }

ajax_salva_session.php file (which saves the text received from the editor):

<?php
    $texto = isset($_POST['texto']) ? $_POST['texto'] : '';
    $texto = str_replace("\n", " ", $texto); //usei essas 3 formas que achei  
    $texto = str_replace("\r", " ", $texto); //na net pra tentar eliminar a quebra
    $texto = preg_replace('/\s/',' ',$texto);//de linha, mas até o momento sem sucesso :/
    $_SESSION['capitulo'] = $texto;
?>

line that arrow the text in the text editor (the syntax is right, it is the syntax that is in the documentation: $("#placeholder"). Editor("setText", "Text")):

$('#paragrafo').Editor('setText', "<?php echo ($_SESSION['capitulo']); ?>");

text generated in the browser (which gives error):

$('#paragrafo').Editor('setText', "<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: _SESSION in C:\wamp\www\index.php on line <i>10</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0058</td><td bgcolor='#eeeeec' align='right'>241440</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>0</td></tr>
</table></font>
");
  • Two things: you’re using session_start(); at the beginning of your code? And this is wrong: <?php echo ($_SESSION['capitulo']); ?>. At the very least <?php echo htmlentities($_SESSION['capitulo']); ?>, otherwise any occurrence of quotation marks will cause you trouble.

  • I was not using, now I put both, session_start was by inattention even, but I did not know this htmlentities, now you see if I find the error, that still persists.

  • Alexandre, take your line break removals with str_replace, and just use this: <?php echo nl2br( htmlentities( $_SESSION['capitulo'] ) ); ?>, depending on the editor can work.

  • i’ve heard of this nl2br, but I think q n can use, because the break of line q occurs from javascript n is a break of line html formatting, and only the way the editor creates html

  • Well, in fact nl2br does not remove breaks, it would be the case to first apply nl2br, and then replace

  • but then why would I use nl2br if my intention is not to create br? and yes REMOVE line breaks?

  • Your intention is to remove the break because it is giving problem. I’ve thought about solving only the problem, respecting the original text (ie make it work with the break). But without knowing the component you are using, everything is a kick. It’s the kind of thing that should just be some silly little thing getting in the way, but it gets hard to understand the situation without looking closely.

  • the q component using is the line control editor, its link is https://github.com/suyati/line-control/blob/master/README.md

  • Is there any way to make it work with these line breaks in javascript? because php na vdd ta throwing Session error in html that would be set in the q use editor..

  • but I don’t understand why my Session_start() session is wrong and I set the session $_SESSION['chapter'] = $text

  • 1

    That’s why I made my first comment. As long as you don’t fix the mistake, you’re not testing anything. Only that we can not know here what is your line 10 , that has the problem. It may be a $ missing, or some ; wrong, etc. If you can comment on what is on your line 10, it might help.

  • 1

    I managed to remove the line break at last! thanks for all the help, your tips helped, I think the biggest problem was I n declared session_start() at the beginning and he saved in Sesssion the php error code, and then gave error all the time, Now I changed what is stored in Session and I was able to fix it by this error, thanks again for the tips @Bacco!

  • 1

    If you were able to solve the problem accept the answer that helped or put a new answer if the solution is not in the answers.

Show 8 more comments

2 answers

2


Try to give a replace thus:

str_replace("> <", "><", $texto);

If this doesn’t work the problem may be with quotation marks. Has a colspan="5", double quotes instead of single quotes. As you opened the double quotes at the beginning, he considers this before the 5 a closure, and the after the 5 a new opening.

Replace in ajax

Try to make a replaceupon receipt of the text, replacing the double quotes with the single.

function SalvaSession(valor){
      $.post( "ajax_salva_session.php",
            {
                'texto' : valor.replace("\"", "\'")  //valor recebe o html do editor de texto
           },
           function (data) {
           }
      );
 }
  • didn’t work out :/ but thanks for the help @Samir!

  • Um, I think this is another one too, that must be why this Undefined variable: _SESSION... appears next to the text coming from the editor

  • i put the code $text = str_replace('"', """, $text);

  • The error continues, now I wonder if the mistake is really for these two things or for something else

  • puts, but it won’t change dynamically? because the generated html is generated by the text editor, and not by me...

  • 1

    This is what I was thinking, I imagine it would be at the time of capture, in ajax, in case. I’ll edit the answer with a possible way, try and see if it works.

  • 1

    I edited, if it doesn’t fit I imagine I don’t know how to help you anymore, but I hope it has served you well so far..

  • <textarea id="paragraph" name="paragraph" onblur=="Salvasession($('#paragraph').Editor('gettext'));"></textarea>

  • the editor q to using is the line control editor, his link is this github.com/suyati/line-control/blob/master/README.Md was here q I found more information about it

  • 1

    I managed to remove the line break at last! thanks for all the help, your tips helped, I think the biggest problem was that I n declared session_start() at the beginning and he saved in Sesssion the wrong code, and then there was error all the time, now I changed what is stored in Sesssion and managed to solve at least that, agr need to see how the editor interprets the code, because ta setando the literal html text and it does not appear all formatted, but thank you very much of qq form @Samirbraga

  • @Alexandremartinsmontebelo, I am happy to know that you had advances... Good luck with the continuation of the problems, I hope you solve them too :) .

Show 6 more comments

2

try like this

$logradouro = htmlentities($logradouro, null, 'utf-8');
$logradouro = str_replace("&nbsp;"," ",$logradouro);
$logradouro = str_replace("\n", "", $logradouro);
$logradouro = str_replace("\r", "", $logradouro);
$logradouro = preg_replace('/\s/',' ',$logradouro);

Browser other questions tagged

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