DeanWhitehouse Posted May 12, 2008 Share Posted May 12, 2008 how can i detect when a user starts a new line in an input form, textarea, i was thinking, doing strlength and then doing a wordwrap, with the strlength. Not the best idea, any others? Quote Link to comment https://forums.phpfreaks.com/topic/105297-solved-how-to-detect-the-end-of-a-line/ Share on other sites More sharing options...
craygo Posted May 12, 2008 Share Posted May 12, 2008 usually the end of the line is designated by "\n". Even though you usually can't see it, it's there. Ray Quote Link to comment https://forums.phpfreaks.com/topic/105297-solved-how-to-detect-the-end-of-a-line/#findComment-539199 Share on other sites More sharing options...
FVxSF Posted May 12, 2008 Share Posted May 12, 2008 You can preg_match for the \n <?php $strText = "This\nis only\na test"; if (preg_match('/\n/', $strText)) { echo "Multi Line"; // Or what ever code you'd like to add. } else { echo "Single Line"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/105297-solved-how-to-detect-the-end-of-a-line/#findComment-539207 Share on other sites More sharing options...
DeanWhitehouse Posted May 12, 2008 Author Share Posted May 12, 2008 well, when i enter stuff into a textarea, and decide to start a new line, it is displayed all on one line , i want it so that when i start a new line, it inputs this new line Quote Link to comment https://forums.phpfreaks.com/topic/105297-solved-how-to-detect-the-end-of-a-line/#findComment-539209 Share on other sites More sharing options...
craygo Posted May 12, 2008 Share Posted May 12, 2008 well if you want to display lines from a text area you have to use nl2br() function. echo nl2br($textarea); Your browser doesn't know \n so you have to convert it to <br> to get it to display correctly Ra Quote Link to comment https://forums.phpfreaks.com/topic/105297-solved-how-to-detect-the-end-of-a-line/#findComment-539215 Share on other sites More sharing options...
DeanWhitehouse Posted May 12, 2008 Author Share Posted May 12, 2008 how do i convert, preg_replace?? If os can someone show me the code? Quote Link to comment https://forums.phpfreaks.com/topic/105297-solved-how-to-detect-the-end-of-a-line/#findComment-539219 Share on other sites More sharing options...
FVxSF Posted May 12, 2008 Share Posted May 12, 2008 craygo has the best solution. nl2br Basically nl2br converts the \n to <br /> <?php echo nl2br("This is\na test"); // Outputs: // This is<br />a test ?> Quote Link to comment https://forums.phpfreaks.com/topic/105297-solved-how-to-detect-the-end-of-a-line/#findComment-539226 Share on other sites More sharing options...
craygo Posted May 12, 2008 Share Posted May 12, 2008 Well first what do you want to do? The person fills out the text area, What then?? Are you storing it into a database?? Do you want to echo the results out as soon as they submit?? Let me know what you want to do. Ray Quote Link to comment https://forums.phpfreaks.com/topic/105297-solved-how-to-detect-the-end-of-a-line/#findComment-539228 Share on other sites More sharing options...
DeanWhitehouse Posted May 12, 2008 Author Share Posted May 12, 2008 The user enters the news, it then posts to a database, it then is displayed through a loop on the main page. but, i need to change the /n to <br > how ?? Quote Link to comment https://forums.phpfreaks.com/topic/105297-solved-how-to-detect-the-end-of-a-line/#findComment-539231 Share on other sites More sharing options...
craygo Posted May 12, 2008 Share Posted May 12, 2008 I assume you have it going to the database just fine. So to display it just use this <?php $sql = "SELECT * FROM table"; $res = mysql_query($sql) or die(mysql_error()); while($r = mysql_fetch_assoc($res)){ echo nl2br(stripslashes($r['textarea'])); } ?> Simple as that. no need to loop through each line. It will automatically convert it and display it properly. I added stripslashes in there for words like it's and such otherwise you will have slashes all over the place. Ray Quote Link to comment https://forums.phpfreaks.com/topic/105297-solved-how-to-detect-the-end-of-a-line/#findComment-539237 Share on other sites More sharing options...
FVxSF Posted May 12, 2008 Share Posted May 12, 2008 I'm not sure how you are saving it to your database but the following function is most likely what you're looking for. http://www.php.net/nl2br Quote Link to comment https://forums.phpfreaks.com/topic/105297-solved-how-to-detect-the-end-of-a-line/#findComment-539239 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.