I have a form box where users enter in directions (one per line). I then turn each line into a list item <li>. The problem is that when people enter a blank line at the end it creates a new list item. So I tried the following preg_replace to strip out extra lines at the end of the string. It doesn't work. $directions = preg_replace('/\r+$/','',$_POST['directions']); Code (markup): I also tried \n instead of \r.
HEhe, you should combine both! THis should works $directions = preg_replace('/[\r\n]+$/','',$_POST['directions']); PHP:
Here's an example, let me know how this works: <?php // string with bunch of CRLF's $my_string = "Liquid\r\nTension Experiment\r\n\r\n\r\n"; // replace CRLF's with spaces $my_wonderful_string = str_replace("\r\n", " ", $my_string); // would result in "Liquid Tension Experiment " // or just delete the CRLF's (by replacing them with nothing) $my_wonderful_string = str_replace("\r\n", "", $my_string); // would result in "LiquidTension Experiment" ?> PHP:
Sorry but I think you miss the topic starter's point. You cannot remove the in-string \r\n. And have you tested my simple code?
I think what you want to do is remove any trailing spaces and/or newlines and/or tabs and then see how long the string is. When you encounter a zero length string, ignore it. Otherwise, put it inside <li></li> tags. Also, you are right to use preg_replace instead of str_replace because it allows you to test for combinations of characters, instead of assuming they will a set type. As a side note, I have some people say using isset($directions[0]) to find out if $directions has content works faster than strlen($directions).
OH, that's works. People seems to be likely the complicate the problem...And I am one one them. Thanks for "opening my eyes"