2012-01-19 12 views
5

Ho una stringa che contiene alcuni caratteri HTML codificata e voglio rimuoverli:Come rimuovere caratteri codificati in HTML da una stringa?

"<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

Cosa hai provato? E cosa è successo quando ci hai provato? –

+0

Puoi aggiungere il risultato previsto? È un po 'difficile da capire di cosa hai veramente bisogno. Non sono sicuro se vuoi rimuoverli o semplicemente decodificarli. Potrebbe essere necessario rimuovere alcuni e decodificare gli altri? – robertodecurnex

risposta

14

Che cosa si vuole fare è fattibile molti modi. Forse è utile capire perché potresti volerlo fare. Solitamente quando voglio rimuovere l'HTML codificato, voglio recuperare il contenuto dell'HTML. Ruby ha alcuni moduli che lo rendono facile.

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) 

che uscite:

<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> 

Se voglio fare un passo più in là e rimuovere i tag, recuperando tutto il testo: uscita

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

Will:

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 

Qual è il posto in cui di solito voglio da ottenere quando vedo quel tipo di corda.

Ruby's CGI rende la codifica e la decodifica HTML facile. La gemma Nokogiri semplifica la rimozione dei tag.

-1

Se è stata assegnata la stringa ad una variabile s, è questo il risultato che si desidera?

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

vorrei suggerire:

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

-1 per suggerire un'espressione regolare senza menzionare le loro limitazioni per HTML. –

+0

@AndrewGrimm Non è HTML a questo punto. È una stringa con il potenziale per essere HTML. –

+0

Ancora, @ theTinMan la tua risposta è decisamente migliore. – Phrogz

0

Penso che il modo più semplice per fare questo sia , supponendo che si desideri utilizzare l'html nella stringa.

raw CGI.unescapeHTML('The string you want to manipulate') 
Problemi correlati