Grabeorama Posted December 21, 2008 Share Posted December 21, 2008 hi :)I have at the moment a small form where you can type what you want, click save, it writes it to a file, and includes the file lower on the page.I would like to make it so that when \n is type, it is replaced by <br /> is there a way of doing this in PHP?Thanks Link to comment Share on other sites More sharing options...
Anman Posted December 21, 2008 Share Posted December 21, 2008 $userinput is what the user wrote. <?php$userinput = str_replace("\n", "<br />", $userinput);?> So if the user inputs:My name is Bob.\nMy last name is Smith.$userinput will become:My name is Bob.<br />My last name is Smith. Link to comment Share on other sites More sharing options...
Ingolme Posted December 21, 2008 Share Posted December 21, 2008 Or just use the nl2br() function: $userInput = nl2br($userInput); Link to comment Share on other sites More sharing options...
Grabeorama Posted December 21, 2008 Author Share Posted December 21, 2008 O.k.Thanks I'll try that Link to comment Share on other sites More sharing options...
Anman Posted December 21, 2008 Share Posted December 21, 2008 Or just use the nl2br() function:$userInput = nl2br($userInput); I wouldn't use that just because it's a very limited function that does what I mention.However, there is nothing wrong with it. Link to comment Share on other sites More sharing options...
Grabeorama Posted December 21, 2008 Author Share Posted December 21, 2008 <?php$userinput = str_replace("\n", "<br />", $userinput);?> I used this, and it worked great! Thanks! Link to comment Share on other sites More sharing options...
Synook Posted December 22, 2008 Share Posted December 22, 2008 I wouldn't use that just because it's a very limited function that does what I mention.However, there is nothing wrong with it.nl2br() may be the more limited of the two functions, but the advantage of it is that it is faster - much faster. In my benchmarking test, it ran over twice as fast as the str_replace() call. See for yourself:<?php header("content-type:text/plain"); $nl2br = array(); $str_replace = array(); for ($j = 1; $j <= 20; $j++) { $then = microtime(true); for ($i = 1; $i <= 100000; $i++) nl2br("\n"); $nl2br[] = microtime(true) - $then; $then = microtime(true); for ($i = 1; $i <= 100000; $i++) str_replace("\n", "<br />", "\n"); $str_replace[] = microtime(true) - $then; } $nl2br = array_sum($nl2br) / count($nl2br); $str_replace = array_sum($str_replace) / count($str_replace); echo "Speeds (in seconds)\n"; echo "nl2br():\t\t$nl2br\n"; echo "str_replace():\t\t$str_replace\n"; echo "Difference:\t\t" . ($nl2br - $str_replace) . "\n"; echo "Ratio:\t\t\t" . ($nl2br / $str_replace);?> Link to comment Share on other sites More sharing options...
Anman Posted December 22, 2008 Share Posted December 22, 2008 nl2br() may be the more limited of the two functions, but the advantage of it is that it is faster - much faster. In my benchmarking test, it ran over twice as fast as the str_replace() call. See for yourself:<?php header("content-type:text/plain"); $nl2br = array(); $str_replace = array(); for ($j = 1; $j <= 20; $j++) { $then = microtime(true); for ($i = 1; $i <= 100000; $i++) nl2br("\n"); $nl2br[] = microtime(true) - $then; $then = microtime(true); for ($i = 1; $i <= 100000; $i++) str_replace("\n", "<br />", "\n"); $str_replace[] = microtime(true) - $then; } $nl2br = array_sum($nl2br) / count($nl2br); $str_replace = array_sum($str_replace) / count($str_replace); echo "Speeds (in seconds)\n"; echo "nl2br():\t\t$nl2br\n"; echo "str_replace():\t\t$str_replace\n"; echo "Difference:\t\t" . ($nl2br - $str_replace) . "\n"; echo "Ratio:\t\t\t" . ($nl2br / $str_replace);?> The problem is nl2br() returns pure HTML, so you must be careful in which order you transform the string.That's why I tell people to use str_replace(), but hey, if you're not getting errors and have no problem concering limits use nl2br() by all means.Oh and if you're using an HTML file as opposed to an XHTML file use nl2br($string, false); for the sake of file space. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.