Php is striping off line breaks even when strip functions were not called

How can i stop php from stripping off lines breaks, i have a simple text in the mysql table and the text has line breaks or paragraph lines, but when I query the database to output the content in the page, the whole paragraph lines will be lost .

Emple:

My work is noe that i can play with the long text

but everything i do went well and tell people that

$search - (qurey the database to get content)


echo htmlSpecialChars($search):
but instead of me to have

My work is noe that i can play with the long text


but everything i do went well and tell people that 

am having:

My work is noe that i can play with the long text
but everything i do went well and tell people that 

Please why is it showing that way
Thank you

Perhaps the following function could be used:

https://www.php.net/manual/de/function.nl2br.php

1 Like

i will check it out once i on my laptop, but still wondering if it will work since there is no n/r or any of the php line breaks tags in the text.

i can put <p>Tex break here</p> or <br> in the content am returning from the sql.
and then use htmlSpecialChars_decode($text) so as to retain the hmtl.

But am having issues when a text or content was copied from notedpad or wordpad with the normal line breaks or paragraph lines that exists in wordpad and or notedpad and paste it like that into the sql database, but when i retrieve it from the database and echo it in the php all lines are gone the text all joined as single text and becomes unreadable.

I think I should clearly state the use case here,
am creating a comment box, which user will type and post the comments to the database and afterwards we will query the database and displays users comment on a page.

Now if user types into the comments form

<textarea name="comment" rows="10" cols="5"></textarea>

And submit, it will be unfair to have all the paragraph and lines the commenter arranged when typing to be striped off when is displayed.
Also is not professional to ask commenters to

use <br> or n/r or <p></p> 

knowing that not all users of the site will be comfortable with codes or such language.

So i need a way around this as to make my textarea form act like WYSIWYG or anything like that.

This isn’t a php issue and the characters are not being stripped. The issue is that new-line characters in text don’t have any meaning in html, so, when you output text in a html context, there’s no visible result. The new-line characters format the source html. If you do a ‘view source’ of the resulting output in a browser, you will see the new-line characters. The linked to nl2br function, used when you output text on a web page, adds a html <br> tag to (before) every new-line character so that you get html line breaks in the rendered output.

1 Like

thanks i know it has nothing to do with php stripping but then I understand why plain text text are not rendering in php but my question is how can I make this to render the way i want.

I just copied same text to a wordpress wp_posts and then viewed the page in the front end and everything was normal as the way the original text was typed with each lines and paragraph still unchanged.

I don’t know exactly how wordpress is rendering such display because normally like you pointed out there is no business with empty spaces in plain text with php or html tags which explains why text are all joining together as one text when rendered.

But can I know how to get the same results wordpress is getting using the way they render such text from database to still retain its normal text lines and paragraph?

Is now that i understood what you meant, and this is how i solved the problem.

$content = 'this is a plain text with paragraph lines

That has no html or new line break';

echo nl2br($content);

The above code works for normal variable written inside the php file, and also work for query or SQL results. but for some reasons is not working in my current project and i have tried to figure it out but didn’t.

So this is what I used to got my own working for sql results

$result = $search->fetch ();

echo stri_ireplace('&#13;&#10', '<br>',  nl2br($result);

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.