Delete line breaks in an echo in PHP

Asked

Viewed 3,583 times

2

I have an html/php page that receives data in a textarea and saves it in a mysql database.

Sometimes the user uses line break in his text and saves in the good database.

I also have a php page that converts any record into json, https://buscafree.com.br/listaDetalhe.php?id=1, that can be read by an android app I’m developing.

However, when there is some line break, the app gives problem reading json.

If you click on the link above, you will see that the key value descricao is like this: Tudo em moda masculina. Os melhores preços você encontra aqui! Venda via Instagram. It does not show here, but after the exclamation, there is a line break before the text 'Sale via Instagram'. It’s not easy to solve, because I don’t know how to "catch" this line break

How to do php to ignore line breaking in a mysql database when generating a echo, example:

echo semQuebraDeLinha($linha['dado']);
  • 1

    Tried to $v = str_replace("\n"," ", $linha['dado']) or preg_replace('/\s/',' ',$string);

  • @RBZ, did not work since line breaks are not saved in the bank by n

  • Then remove in select with replace... tried ?

  • I used the function nl2br() that added in the text stored in the BD Mysql the text with several <br />. Even so, next to these breaks, line breaks still persisted... then, after the above function, I used str_replace(), changing the breaks - char(10) - by <br>... the text was then stored with two lines breaks <br /> and <br>, next to each other, meaning that even using the nl2br function, still persisted a line break type Chr(10)... to return the text to the screen with only one break per line, I used str_replce to remove one of the <br>.

  • See this other one too. Replacing the char(10)...

  • You tried to preg_replace like this: preg_replace( "/\r|\n/", "", $linha['dado'] );

  • the point is that the text appears normal, with no symbol indicating the line break :/

  • add the question as to the text

  • @Added guilhermecostamilam

  • @RBZ think it should work with char(10), but I’m not understanding how to use it. can give an example?

  • In mysql it appears 2 strange "squares". I already asked a question and put an image of it. I’m on my cell phone, I can’t test it now, but tomorrow I’ll test it and tell you.

  • 1

    Sorry guys, I solved with the @Fabianomonteiro tip: preg_replace( "/ r| n/", "", $line['given'] ); If you want to receive the points just post the answer. hug

Show 7 more comments

1 answer

3


Then try to perform a search for a regular expression and replace it by cleaning \r car return ("Carriage Return") and \n Newline. Behold:

preg_replace( "/\r|\n/", "", $linha['dado'] );

If the match is found, the new Subject($line['given']) will be returned, otherwise Subject will be returned unchanged or NULL if an error occurred. source: php.net

Browser other questions tagged

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