Jump to content

Auto Linebreaks?


Sniffy

Recommended Posts

I have a form, and users can write to the text file, but if they don't know html they'll mess it up by not using linebreaks, how can I use like str_replace() function to replace linebreaks in the form with <br /> tags to make the linebreaks?

Link to comment
Share on other sites

A slightly altered example from the manual of str_replace()

$str = $_POST['textbox'];$order  = array("\r\n", "\n", "\r");$replace = '<br />';str_replace($order, $replace, $str);

That is of course if we suppose the ID of your textbox is "textbox". This particular conversion should work no matter the OS of the client.

Link to comment
Share on other sites

\n is a newline character and \r is a linefeed character. Different operating systems use different combinations for their line endings. The array is replacing all three versions with a <br /> tag. In the order of the array, it first replaces \r\n, which is used by PCs, then \n which is used by Unix, then \r which is used by Mac.

Link to comment
Share on other sites

Does nl2br() covert all new line variants the way my example does or does it only covert \n only?

Link to comment
Share on other sites

I can't really tell from the documentation. The documentation just says "newline", which I would assume only mean \n. But, then again, if it had some \r\n and only replaced the \n, then it would be \r<br>, and I think Windows might (and Mac would) still treat the \r as a newline.I guess you could test it out though. Make a string that is "\r\n", check the length on it to make sure the length is 2, and then do nl2br on it. Print the new string and check the length, if there are any extra characters other then the "<br />" then it missed the \r.Anyway, here is a function from the nl2br reference page. People were saying that the regular expression is a little quicker then using str_replace:

function nl2brr($text){   return preg_replace("/\r\n|\n|\r/", "<br />", $text);}

Link to comment
Share on other sites

Now the question I'm wondering is whether POSIX or Perl regular expressions in PHP are faster...Should one use

function nl2brr($text){   return preg_replace("/\r\n|\n|\r/", "<br />", $text);}

or is it better

function nl2brr($text){   return ereg_replace("\r\n|\n|\r", "<br />", $text);}

Link to comment
Share on other sites

<?phpini_set("max_execution_time", 300);$str = "This is \n a test \r string \r\n";$i = 0;while ($i++ < 1000000){  $time1 = nl2brr_p($str);  $time2 = nl2brr_e($str);}echo "preg_replace: {$time1} seconds<br>";echo "ereg_replace: {$time2} seconds<br>";function nl2brr_p($text){   static $time = 0;   $start = microtime(true);   $retval = preg_replace("/\r\n|\n|\r/", "<br />", $text);   $time += (microtime(true) - $start);   return $time;}function nl2brr_e($text){   static $time = 0;   $start = microtime(true);   $retval = ereg_replace("\r\n|\n|\r", "<br />", $text);   $time += (microtime(true) - $start);   return $time;}?>

preg_replace: 44.953635692596 secondsereg_replace: 12.375408649445 seconds
It looks like ereg wins by quite a bit over 1 million iterations with 3 replacements.
Link to comment
Share on other sites

There really isn't a function tutorial. There is a function reference though, and you can probably read some pretty good books that talk about how to do various things. But the PHP function reference is probably the #1 site I go to when I'm developing PHP:http://www.php.net/manual/en/funcref.phpThe functions that probably get used most often would be the Array, Date/Time, Directory, Filesystem, FTP, HTTP, Mail, Math, Misc, MySQL, Network, Regular Expressions (both POSIX and Perl-compatible), Session Handling, String, and URL functions.For regular expressions, everything is on the PCRE reference page:http://www.php.net/manual/en/ref.pcre.phpDown in the table of contents, you'll see links for Pattern Modifiers and Pattern Syntax. You can read there to get an overview, and do a search for regular expression tutorials if you want to know more. Regular expressions are a very powerful and very complex tool.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...