2012-01-19 7 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 

내가 보통 원하는 곳은 어느 곳입니까? 내가 그 종류의 문자열을 볼 때 얻을 수 있습니다.

루비의 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') 
관련 문제