php won't let me print a new line, format specifier?

  • aJack7
  • Newbie
  • Newbie
  • aJack7
  • Posts: 5

Post 3+ Months Ago

Hi guys,

I have a brief question since its driving me mad. I haven't done any PHP for ages but I'm getting back into it however whenever I'm using echo to attempt to print new line ie.

Code: [ Select ]
echo("text\ntext");


The example does not print a newline, in fact its completely ignored ie. output -> texttext, has this something to do with php5?. I can't for the life of me figure out why this is happening, this method worked fine with php4, whats changed?.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

Newlines won't show up in rendered HTML output; you need to use an HTML line break:

Code: [ Select ]
echo("text<br/>text");


Alternatively, you can run your text through nl2br() before rendering it, which inserts line breaks before all newlines:

Code: [ Select ]
$text = "text\ntext";
$text = nl2br($text);
echo($text);
  1. $text = "text\ntext";
  2. $text = nl2br($text);
  3. echo($text);


If you look at the source code to the rendered page when using just newlines, you'll see that the newline actually is there; the browser simply ignores it.
  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

Little difference there ^^

exmpl1. Echo "FirstText<br>NextText";
exmpl2. Echo "FirstText<br>\nNextText";

To see the difference between these two you need to open the source of the html page ;-)

exmpl1.
Source.
FirstText<br>NextText

Browser output.
FirstText
NextText

exmpl2.
source.
FirstText<br>
NextText

Browser output.
FirstText
NextText

the key to this issue is that the browser wont render any \r\n as an actual visual new line in the rendered html output. It will intepret it as an new line in the source code of the received html document. For this reason, because sometimes like in textboxes you actually want the \r\n to be translated to a visual break (<br>) they introduced the nl2br() function as shown in the previous post.

dont forget that PHP is an pre-hypertext-processor. PHP generates "html-source" code, not the final visual output. that part is done by the html browser ^^
  • maximusss
  • Born
  • Born
  • maximusss
  • Posts: 2

Post 3+ Months Ago

Hi everyone,

I would like to write a tool similar to http://webutils.pl/nl2br but without the duplicate <br> effect when submitting a few times in a row.

Is there a built-in function that can do that ?
  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

There isnt a "build-in" function available as far as i know. NL2BR does just that, convert \n to <br>. So you can either "strip" the \n from the string after applying the <br>. Or you can evaluate the string rule by rule writing your own function, that can look something like this.. (be aware its just a code sniplet that might still require some work)

Code: [ Select ]
 
 
function myNL2BR($string){
   if(!empty($string)){
        $line_array = explode('\n',$string);
        $result = '';
       
        foreach($line_array as $key => $value){
          if(strstr(substr($value, -10), '<br>')){
             $result .= $value;
          }else{
             $result .= str_replace('\n', '<br>', $value)
        }
        if(!empty($result)){
           return $result;
        }else{
           return false;
        }
   }else{
      return false;
   }
}
 
 
 
 
  1.  
  2.  
  3. function myNL2BR($string){
  4.    if(!empty($string)){
  5.         $line_array = explode('\n',$string);
  6.         $result = '';
  7.        
  8.         foreach($line_array as $key => $value){
  9.           if(strstr(substr($value, -10), '<br>')){
  10.              $result .= $value;
  11.           }else{
  12.              $result .= str_replace('\n', '<br>', $value)
  13.         }
  14.         if(!empty($result)){
  15.            return $result;
  16.         }else{
  17.            return false;
  18.         }
  19.    }else{
  20.       return false;
  21.    }
  22. }
  23.  
  24.  
  25.  
  26.  


or something like that ^^

Grtz
  • maximusss
  • Born
  • Born
  • maximusss
  • Posts: 2

Post 3+ Months Ago

Thanks a lot.

I will write my own function as you suggested.

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 137 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.