Sniffy Posted December 16, 2006 Share Posted December 16, 2006 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 More sharing options...
boen_robot Posted December 16, 2006 Share Posted December 16, 2006 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 More sharing options...
reportingsjr Posted December 16, 2006 Share Posted December 16, 2006 Or, you can use the way easier version: $str = nl2br($_POST['textbox']); Link to comment Share on other sites More sharing options...
Sniffy Posted December 17, 2006 Author Share Posted December 17, 2006 Thanks, but can Boen Robot explain his version to me. I'm not too up on the uses of arrays and I don't know what \r means. Link to comment Share on other sites More sharing options...
justsomeguy Posted December 18, 2006 Share Posted December 18, 2006 \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 More sharing options...
Sniffy Posted December 19, 2006 Author Share Posted December 19, 2006 I see, very clever.nl2br() is the way to go for me though... Link to comment Share on other sites More sharing options...
boen_robot Posted December 19, 2006 Share Posted December 19, 2006 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 More sharing options...
justsomeguy Posted December 19, 2006 Share Posted December 19, 2006 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 More sharing options...
skym Posted December 20, 2006 Share Posted December 20, 2006 nl2br() actually adds <br /> to \r\n (or to \n or to \r), it does not replace them. When counting the length of the resulting string, it will always be 6 characters longer. Link to comment Share on other sites More sharing options...
Sniffy Posted December 20, 2006 Author Share Posted December 20, 2006 Cool, so it's better to use justsomeguy's method to conserve file size? Link to comment Share on other sites More sharing options...
boen_robot Posted December 20, 2006 Share Posted December 20, 2006 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 More sharing options...
justsomeguy Posted December 20, 2006 Share Posted December 20, 2006 <?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 secondsIt looks like ereg wins by quite a bit over 1 million iterations with 3 replacements. Link to comment Share on other sites More sharing options...
Sniffy Posted December 20, 2006 Author Share Posted December 20, 2006 Just some PHP Questons:Can I have some good tutorials to the various functions of PHP, and regular expressions?I've used simple flags in JS but never knew about using regular expressions. Link to comment Share on other sites More sharing options...
justsomeguy Posted December 20, 2006 Share Posted December 20, 2006 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 More sharing options...
Sniffy Posted December 20, 2006 Author Share Posted December 20, 2006 Bookmarked. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now