2012-01-19 23 views
5

私はいくつかのHTMLエンコードされた文字を含む文字列を持っていると私はそれらを削除する:文字列からHTMLエンコード文字を削除するにはどうすればよいですか?

あなたは変数 sにその文字列を割り当てている場合
"<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

何を試しましたか?そして、あなたが試したときに何が起こったのですか? –

+0

期待どおりの結果を追加できますか?本当に必要なものを理解するのは難しいです。あなたがそれらを削除したいのか、それとも単に解読するのかはわかりません。あなたはいくつかを削除し、他の人を解読する必要がありますか? – robertodecurnex

答えて

14

はなんとか多くの方法です。あなたがそれをしたいと思うかもしれない理由を見て、おそらく助けになるでしょう。通常、エンコードされたHTMLを削除する場合は、HTMLのコンテンツを復元したいと考えています。 Rubyには簡単なモジュールがいくつかあります。

出力
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) 

<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 Nokogiri::HTML(CGI.unescapeHTML(html)).content 

ウィル出力:

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 

私はいつも好きです私がそのような文字列を見たときに取得する。

RubyのCGIは、HTMLのエンコードとデコードを容易にします。 Nokogiriの宝石を使用すると、タグを簡単に取り外すことができます。

-1

、これはあなたが望む結果ですか?

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

私が示唆している:あなたが何をしたいか

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

-1は、HTMLの制限を言及せずに正規表現を提案するためのものです。 –

+0

@AndrewGrimmこの時点ではHTMLではありません。 HTMLである可能性のある文字列です。 –

+0

まだ、@ theTinManあなたの答えは良い方法です。 – Phrogz

0

私は最も簡単なこれは、あなたが文字列にhtmlを使いたいと思うと思います。

raw CGI.unescapeHTML('The string you want to manipulate') 
関連する問題