Perad Posted January 31, 2008 Share Posted January 31, 2008 How do you detect a new line in an input box and wrap the content in paragraph tags? Quote Link to comment https://forums.phpfreaks.com/topic/88707-wrap-paragraphs-in-tags/ Share on other sites More sharing options...
pdkv2 Posted January 31, 2008 Share Posted January 31, 2008 Use the php function nl2br() this will convert the new line to <br> tag Regards Sharad Quote Link to comment https://forums.phpfreaks.com/topic/88707-wrap-paragraphs-in-tags/#findComment-454232 Share on other sites More sharing options...
nethnet Posted January 31, 2008 Share Posted January 31, 2008 The function nl2br() will replace all new lines with <br /> tags, but if you need to put the content inside <p> tags, then you could try the following: <?php $raw_content = $_GET['textbox']; $lines = explode("\r\n",$raw_content); $formatted = array(); foreach ($lines AS $line){ if (!empty($line)) $formatted[] = "<p>" . $line . "</p>"; } ?> The $formatted array will then contain each line from the form inside <p> tags. Also, this will work if the content is coming from a <textarea> because the form uses both \r and \n for new lines. If this doesn't work, play around with "\r", "\n", and "\r\n" to see which works if you aren't working with a <textarea>. Theo Quote Link to comment https://forums.phpfreaks.com/topic/88707-wrap-paragraphs-in-tags/#findComment-454235 Share on other sites More sharing options...
Perad Posted January 31, 2008 Author Share Posted January 31, 2008 Thanks a lot I will give it a go. Quote Link to comment https://forums.phpfreaks.com/topic/88707-wrap-paragraphs-in-tags/#findComment-454237 Share on other sites More sharing options...
resago Posted January 31, 2008 Share Posted January 31, 2008 preg_replace('/(.*)\n/','<p>$1<\/p>',$data); Quote Link to comment https://forums.phpfreaks.com/topic/88707-wrap-paragraphs-in-tags/#findComment-454846 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.