Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Close Forgotten HTML Tags Easily In User HTML Input

DZone's Guide to

Close Forgotten HTML Tags Easily In User HTML Input

·
Free Resource
If you let users type raw HTML into your input boxes, they will inevitably forget to close some of their tags from time to time. Close any forgotten tags with this:

h1 = {}
h2 = {}
code.scan(/\<([^\>\s\/]+)[^\>\/]*?\>/).each { |t| h1[t[0]] ? h1[t[0]] += 1 : h1[t[0]] = 1 }
code.scan(/\<\/([^\>\s\/]+)[^\>]*?\>/).each { |t| h2[t[0]] ? h2[t[0]] += 1 : h2[t[0]] = 1 }
h1.each {|k,v| code += "
  " * (h1[k] - h2[k].to_i) if h2[k].to_i < v }
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}