开发者

Storing and Displaying TextArea user inputs

开发者 https://www.devze.com 2022-12-20 02:57 出处:网络
How can i handle user inputs with textarea, i need to strip user entered html tags, store text somewhere and display it back in a web开发者_开发百科page.

How can i handle user inputs with textarea, i need to strip user entered html tags, store text somewhere and display it back in a web开发者_开发百科page. I also need to take care about line breaks

Any best practices without using <pre> tag ?


You can always do a find-replace of \n with <br /> to preserve line breaks.

However, stripping html is a bit trickier. The easiest thing to do is replace < and > with &lt; and &gt;. But that doesn't actually strip the html, it merely forces it to render as plain text instead of html.

You could use a regex replace to remove <anything> but that has many potential pitfalls.


if you're using PHP, you can always use the nl2br() function to display the text back on the page.


I created a function called SafeComment designed to eliminate the problem characters from the input for SQL, javascript, HTML and VB. Since our sites and code are almost all VB & VB script. It's function is to allow any freeform input field to be successfully received, processed, saved and displayed. We needed it to maintain PCI compliance. It's not pretty, but it works.

Function SafeComment(ByVal strInput)
' Renders Any Comment Codes Harmless And Leaves Them HTML readable In An eMail Or Web Page
' Try: SafeComment("`~!@#$%^&*()_+=-{}][|\'"";:<>?/.,")
    SafeComment = ""
    If Len(strInput) = 0 Then Exit Function
    SafeComment =   Replace( _
                    Replace(Replace(Replace( _
                    Replace(Replace(Replace( _
                    Replace(Replace(Replace( _
                    Replace(Replace(Replace( _
                    Server.HtmlEncode(Trim(strInput)), _
                    ":", "&#58;"), "-", "&#45;"), "|", "&#124;"), _
                    "`", "&#96;"), "(", "&#40;"), ")", "&#41;"), _
                    "%", "&#37;"), "^", "&#94;"), """", "&#34;"), _
                    "/", "&#47;"), "*", "&#42;"), "\", "&#92;"), _
                    "'", "&#39;")
End Function
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号