2012-01-19 35 views
5

Tôi có một chuỗi có chứa một số HTML mã hóa ký tự và tôi muốn để loại bỏ chúng:Làm cách nào để xóa các ký tự được mã hóa HTML khỏi chuỗi?

"<div>Hi All,</div><div class=\"paragraph_break\">< /></div><div>Starting today we are initiating PoLS.</div><div class=\"paragraph_break\"><br /></div><div>Please use the following communication protocols:<br /></div><div>1. Task Breakup and allocation - Gravity<br /></div><div>2. All mail communications - BC messages<br /></div><div>3. Reports on PoC/Spikes: Writeboard<br /></div><div>4. Non story related tasks: BC To-Do<br /></div><div>5. All UI and HTML will communicated to you through BC.<br /></div><div>6. For File sharing, we'll be using Dropbox.<br /></div><div>7. Use Skype for lighter and generic desicussions. However, in case you need any approvals, data for later reference, etc, then please use BC. PoLS conversation has been created on skype.</div><div class=\"paragraph_break\"><br /></div><div>You'll have been given necessary accesses to all these portals. Please start using them judiciously.</div><div class=\"paragraph_break\"><br /></div><div>All the best!</div><div class=\"paragraph_break\"><br /></div><div>Thanks,<br /></div><div>Saurav<br /></div>" 
+1

Bạn đã thử gì? Và chuyện gì đã xảy ra khi bạn thử? –

+0

Bạn có thể thêm kết quả mong muốn không? Là loại khó hiểu những gì bạn thực sự cần. Không chắc chắn nếu bạn muốn xóa chúng hoặc chỉ giải mã chúng. Có thể bạn cần phải loại bỏ một số và giải mã những người khác? – robertodecurnex

Trả lời

14

Những gì bạn muốn làm là nhiều cách doable. Có lẽ nhìn vào lý do tại sao bạn có thể muốn làm điều đó sẽ giúp. Thông thường khi tôi muốn xóa HTML được mã hóa, tôi muốn khôi phục nội dung của HTML. Ruby có một số mô-đun giúp dễ dàng.

require 'cgi' 
require 'nokogiri' 

html = "<div>Hi All,</div><div class=\"paragraph_break\">< /></div><div>Starting today we are initiating PoLS.</div><div class=\"paragraph_break\"><br /></div><div>Please use the following communication protocols:<br /></div><div>1. Task Breakup and allocation - Gravity<br /></div><div>2. All mail communications - BC messages<br /></div><div>3. Reports on PoC/Spikes: Writeboard<br /></div><div>4. Non story related tasks: BC To-Do<br /></div><div>5. All UI and HTML will communicated to you through BC.<br /></div><div>6. For File sharing, we'll be using Dropbox.<br /></div><div>7. Use Skype for lighter and generic desicussions. However, in case you need any approvals, data for later reference, etc, then please use BC. PoLS conversation has been created on skype.</div><div class=\"paragraph_break\"><br /></div><div>You'll have been given necessary accesses to all these portals. Please start using them judiciously.</div><div class=\"paragraph_break\"><br /></div><div>All the best!</div><div class=\"paragraph_break\"><br /></div><div>Thanks,<br /></div><div>Saurav<br /></div>" 

puts CGI.unescapeHTML(html) 

mà kết quả đầu ra:

<div>Hi All,</div><div class="paragraph_break">< /></div><div>Starting today we are initiating PoLS.</div><div class="paragraph_break"><br /></div><div>Please use the following communication protocols:<br /></div><div>1. Task Breakup and allocation - Gravity<br /></div><div>2. All mail communications - BC messages<br /></div><div>3. Reports on PoC/Spikes: Writeboard<br /></div><div>4. Non story related tasks: BC To-Do<br /></div><div>5. All UI and HTML will communicated to you through BC.<br /></div><div>6. For File sharing, we'll be using Dropbox.<br /></div><div>7. Use Skype for lighter and generic desicussions. However, in case you need any approvals, data for later reference, etc, then please use BC. PoLS conversation has been created on skype.</div><div class="paragraph_break"><br /></div><div>You'll have been given necessary accesses to all these portals. Please start using them judiciously.</div><div class="paragraph_break"><br /></div><div>All the best!</div><div class="paragraph_break"><br /></div><div>Thanks,<br /></div><div>Saurav<br /></div> 

Nếu tôi muốn mang nó một bước xa hơn và loại bỏ các thẻ, lấy tất cả các văn bản:

puts Nokogiri::HTML(CGI.unescapeHTML(html)).content 

Will đầu ra:

Hi All,Starting today we are initiating PoLS.Please use the following communication protocols:1. Task Breakup and allocation - Gravity2. All mail communications - BC messages3. Reports on PoC/Spikes: Writeboard4. Non story related tasks: BC To-Do5. All UI and HTML will communicated to you through BC.6. For File sharing, we'll be using Dropbox.7. Use Skype for lighter and generic desicussions. However, in case you need any approvals, data for later reference, etc, then please use BC. PoLS conversation has been created on skype.You'll have been given necessary accesses to all these portals. Please start using them judiciously.All the best!Thanks,Saurav 

Đó là nơi tôi thường muốn để có được khi tôi thấy loại chuỗi đó.

Ruby CGI giúp mã hóa và giải mã HTML dễ dàng. Đá quý Nokogiri giúp bạn dễ dàng xóa thẻ.

-1

Nếu bạn đã gán chuỗi cho một biến s, đây là kết quả bạn muốn?

puts s.gsub(/&lt;[^&]*&gt;/, '') 
0

tôi sẽ đề nghị:

clean = str.gsub /&lt;.+?&gt;/, '' 
+0

-1 để đề xuất một regex mà không đề cập đến giới hạn của chúng đối với HTML. –

+0

@AndrewGrimm Nó không phải là HTML vào thời điểm này. Đó là một chuỗi có khả năng là HTML. –

+0

Tuy nhiên, @theTinMan câu trả lời của bạn là tốt hơn. – Phrogz

0

Tôi nghĩ rằng dễ nhất cách để làm điều này là, Giả sử bạn muốn sử dụng html trong chuỗi.

raw CGI.unescapeHTML('The string you want to manipulate') 
Các vấn đề liên quan