Come convertire HTML in testo in SQL

May 28

Come convertire HTML in testo in SQL


Conversione HTML in testo in SQL può essere fatto in modo efficiente con una funzione definita dall'utente. Una funzione definita dall'utente accetta parametri, esegue un'azione, come ad esempio l'analisi HTML, e restituisce il risultato come un valore. La funzione può essere eseguito da qualsiasi istruzione SQL o linguaggio di programmazione esterno.

istruzione

1 Connettersi al database e creare un nuovo file SQL.

2 Inserire il seguente SQL:

CREATE FUNCTION [dbo]. [CleanHTML]

(

@DirtyText Varchar (max)

)

RESI varchar (max)

COME

INIZIO

DICHIARARE @BeginPos int

DICHIARARE @EndPos int

DICHIARARE @Len int

- Sostituire l'entità HTML e con il carattere '&' (questo deve essere fatto prima, come

- 'E' potrebbe essere doppio codificato come '&')

SET @BeginPos = CHARINDEX ( '&', @DirtyText)

SET @EndPos = @BeginPos + 4

SET @ Len = (@EndPos - @BeginPos) + 1

WHILE (@BeginPos> 0 e @EndPos> 0 e @Len> 0) BEGIN

SET @DirtyText = Stuff (@DirtyText, @BeginPos, @Length, '&')

SET @BeginPos = CHARINDEX ( '&', @DirtyText)

SET @EndPos = @BeginPos + 4

SET @ Len = (@EndPos - @BeginPos) + 1

FINE

- Sostituire l'entità HTML <con il '<' carattere

@BeginPos SET = CHARINDEX ( '<', @DirtyText)

SET @EndPos = @BeginPos + 3

SET @ Len = (@EndPos - @BeginPos) + 1

WHILE (@BeginPos> 0 e @EndPos> 0 e @Len> 0) BEGIN

SET @DirtyText = Stuff (@DirtyText, @BeginPos, @Length, '<')

@BeginPos SET = CHARINDEX ( '<', @DirtyText)

SET @EndPos = @BeginPos + 3

SET @ Len = (@EndPos - @BeginPos) + 1

FINE

- Sostituire l'entità HTML> con il '>' carattere

@BeginPos SET = CHARINDEX ( '>', @DirtyText)

SET @EndPos = @BeginPos + 3

SET @ Len = (@EndPos - @BeginPos) + 1

WHILE (@BeginPos> 0 e @EndPos> 0 e @Len> 0) BEGIN

SET @DirtyText = Stuff (@DirtyText, @BeginPos, @Length, '>')

@BeginPos SET = CHARINDEX ( '>', @DirtyText)

SET @EndPos = @BeginPos + 3

SET @ Len = (@EndPos - @BeginPos) + 1

FINE

- Sostituire l'entità HTML e con il carattere '&'

SET @BeginPos = CHARINDEX ( '&', @DirtyText)

SET @EndPos = @BeginPos + 4

SET @ Len = (@EndPos - @BeginPos) + 1

WHILE (@BeginPos> 0 e @EndPos> 0 e @Len> 0) BEGIN

SET @DirtyText = Stuff (@DirtyText, @BeginPos, @Length, '&')

SET @BeginPos = CHARINDEX ( '&', @DirtyText)

SET @EndPos = @BeginPos + 4

SET @ Len = (@EndPos - @BeginPos) + 1

FINE

- Sostituire l'entità HTML con il '' carattere

@BeginPos SET = CHARINDEX ( '', @DirtyText)

SET @EndPos = @BeginPos + 5

SET @ Len = (@EndPos - @BeginPos) + 1

WHILE (@BeginPos> 0 e @EndPos> 0 e @Len> 0) BEGIN

SET @DirtyText = Stuff (@DirtyText, @BeginPos, @Length, '')

@BeginPos SET = CHARINDEX ( '', @DirtyText)

SET @EndPos = @BeginPos + 5

SET @ Len = (@EndPos - @BeginPos) + 1

FINE

- Sostituire le etichette con un ritorno a capo

@BeginPos SET = CHARINDEX ( '', @DirtyText)

SET @EndPos = @BeginPos + 3

SET @ Len = (@EndPos - @BeginPos) + 1

WHILE (@BeginPos> 0 e @EndPos> 0 e @Len> 0) BEGIN

SET @DirtyText = Stuff (@DirtyText, @BeginPos, @Length, CHAR (13) + CHAR (10))

@BeginPos SET = CHARINDEX ( '', @DirtyText)

SET @EndPos = @BeginPos + 3

SET @ Len = (@EndPos - @BeginPos) + 1

FINE

- Sostituire le etichette <br/> con un ritorno a capo

@BeginPos SET = CHARINDEX ( '<br/>', @DirtyText)

SET @EndPos = @BeginPos + 4

SET @ Len = (@EndPos - @BeginPos) + 1

WHILE (@BeginPos> 0 e @EndPos> 0 e @Len> 0) BEGIN

SET @DirtyText = Stuff (@DirtyText, @BeginPos, @Length, 'CHAR (13) + CHAR (10)')

@BeginPos SET = CHARINDEX ( '<br/>', @DirtyText)

SET @EndPos = @BeginPos + 4

SET @ Len = (@EndPos - @BeginPos) + 1

FINE

- Sostituire i tag <br /> con un ritorno a capo

@BeginPos SET = CHARINDEX ( '<br />', @DirtyText)

SET @EndPos = @BeginPos + 5

SET @ Len = (@EndPos - @BeginPos) + 1

WHILE (@BeginPos> 0 e @EndPos> 0 e @Len> 0) BEGIN

SET @DirtyText = Stuff (@DirtyText, @BeginPos, @Length, 'CHAR (13) + CHAR (10)')

@BeginPos SET = CHARINDEX ( '<br />', @DirtyText)

SET @EndPos = @BeginPos + 5

SET @ Len = (@EndPos - @BeginPos) + 1

FINE

- Togliere qualcosa tra i tag <qualsiasi>

@BeginPos SET = CHARINDEX ( '<', @DirtyText)

@EndPos SET = CHARINDEX ( '>', @DirtyText, CHARINDEX ( '<', @DirtyText))

SET @ Len = (@EndPos - @BeginPos) + 1

WHILE (@BeginPos> 0 e @EndPos> 0 e @Len> 0) BEGIN

SET @DirtyText = Stuff (@DirtyText, @BeginPos, @Length, '')

@BeginPos SET = CHARINDEX ( '<', @DirtyText)

@EndPos SET = CHARINDEX ( '>', @DirtyText, CHARINDEX ( '<', @DirtyText))

SET @ Len = (@EndPos - @BeginPos) + 1

FINE

RITORNO LTRIM (RTRIM (@DirtyText))

FINE

3 Compilare la funzione SQL.

4 Eseguire la funzione, e verificare che restituisce i risultati desiderati. Per esempio:

Selezionare da dbo.CleanHTML ( '<html> <body> test </ BODY> </ HTML>');