Fergal Posted September 13, 2011 Share Posted September 13, 2011 I've been reading the section of the book on how to use the following functions; htmlspecialchars() htmlentities() strip_tags() My question is, is it possible to use those functions to deal with HTML tags included in text entered into a form, but still maintain the line breaks in the form data. For example if the following was entered into a textarea on a form; This <strong>is</strong> a line This is another line How could the functions above be used so that when the data is printed to the browser it looks like this; This is a line This is another line Thanks! Link to comment Share on other sites More sharing options...
Jonathon Posted September 13, 2011 Share Posted September 13, 2011 strip_tags() takes a second parameter for allowable tags. So this code: <?php $string = "This <strong>is</strong> a line<br />This is another line"; echo strip_tags($string, '<br>'); /* Outputs: * This is a line * This is another line */ ?> Strips the <strong> tags and allows the <br /> Is that what you were after? 1 Link to comment Share on other sites More sharing options...
Fergal Posted September 13, 2011 Author Share Posted September 13, 2011 Thanks Jonathon, that's exactly what I was looking for. Link to comment Share on other sites More sharing options...
Fergal Posted September 14, 2011 Author Share Posted September 14, 2011 I've just seen in the book that there is another way of doing this and that is by applying the nl2br function after the strip_tags function, as in; $string = strip_tags($string); $string = nl2br($string); Link to comment Share on other sites More sharing options...
Jonathon Posted September 14, 2011 Share Posted September 14, 2011 You can use that function also, you will see that there are often many different ways to achieve the same effect. As you read more of Larry's book(s) you'll see you can introduce applications that work similar to this form posting submission area with formatting options. Link to comment Share on other sites More sharing options...
Fergal Posted September 15, 2011 Author Share Posted September 15, 2011 Thanks Jonathon, I have a lot to learn Link to comment Share on other sites More sharing options...
Jonathon Posted September 15, 2011 Share Posted September 15, 2011 Don't we all in general 1 Link to comment Share on other sites More sharing options...
Recommended Posts