If I have a text input box for input of various features of an item is there an easy way to parse the text field for carrige returns before inserting it into my database table.
What im trying to do is parse the entry and add <li>'s at the beginning of each new line to correctly format the information pulled from the database. So i guess really what im asking is what character is retured in the textarea when a line break is encountered?
Thanks
Jesse
nl2br() is one function you could use, or write a regex to do it for ya.
amc
depends on the OS: Macintosh: \r Unix: \n Windows: \r\n
i suggest you store the data in the database intact (with the newline characters) and do all of your formatting when extracting data from database later. If all you need to do is add <br>'s then you can use nl2br(). If you want to do more advanced formatting (like <ul>, <li>) then you can do some regex stuff to replace the newlines with the proper HTML.
This should get you there. It's untested and maybe a little inefficient, but it should work for you.
// the char your parsing for $delimit="\n"; // $posted_data is what you call the form input box $tok=$posted_data; // loop the posted data looking for $delimit $tok = strtok($string, $delimit); while ($tok) { // build an output $output.="<li></li>".$tok; // reset the loop $tok = strtok($delimit); } // don't forget the last one $output.="<li></li>".$tok; // show the results echo $output;
might want to add the $delimit back for later, look for this line above:
// build an output $output.="<li></li>".$tok;
and replace it with this:
// build an output $output.="<li></li>".$tok.$delimit;
Originally posted by devinemke depends on the OS: Macintosh: \r Unix: \n Windows: \r\n
The Internet standard for transmitting line enders is CR-LF pairs, regardless of what conventions may be followed by host platforms internally. The W3C has included an advisory notice in the HTML standard since at least the HTML3 days indicating that browser developers should normalize textareas to CR-LF format before transmitting the data.