echo removes white spaces from the text and stored text shows up w/o line breaks .. How can i correct this ?

When i view my db tables and contents in it using phpmyadmin, the text stored in them look normal .. the way i typed it .. but when i try to echo or print it out .. it does print the white spaces and everything print out together and confusing ... Is there anyway to correct this. ? Any help would be appreciated ..

Recommended Answers

All 3 Replies

nevermind .. preg_Replace did the trick ..

$new_text = preg_replace('[\n]', '<br>', $text);

if there's and easier way it would be nice to know . .thnax anyway

You could also use

$newtext = nl2br($text);

which has been around since PHP 4 for this purpose.

BTW this problem - if I get it correctly - does not originate from echo 's behaviour, but rather from HTML, which does ignore all whitespace by default. So you have to substitute all \n with <br /> in order to have the line breaks in your HTML output, too (e.g. by using your preg_replace solution or nl2br ).

Happy coding! :)

You could also use

$newtext = nl2br($text);

which has been around since PHP 4 for this purpose.

BTW this problem - if I get it correctly - does not originate from echo 's behaviour, but rather from HTML, which does ignore all whitespace by default. So you have to substitute all \n with <br /> in order to have the line breaks in your HTML output, too (e.g. by using your preg_replace solution or nl2br ).

Happy coding! :)

Cool thanx man .. peace

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.