Conversione di 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, ad esempio l'analisi di HTML e restituisce il risultato come valore. La funzione può essere eseguita da qualsiasi istruzione SQL o il linguaggio di programmazione esterno.
Istruzioni
1
Connettersi al database e creare un nuovo file SQL.
2
Immettere l'istruzione SQL seguente:
CREATE FUNCTION [dbo]. [CleanHTML]
(
@DirtyText varchar (max)
)
Restituisce varchar (max)
COME
BEGIN
DICHIARARE int @BeginPos
DICHIARARE int @EndPos
DICHIARARE int @Len
-Sostituire l'entità HTML & con il carattere '&' (questo deve essere fatto in primo luogo, come
... ' &' potrebbe essere doppia codificati come '&')
SET @BeginPos = CHARINDEX ('&', @DirtyText)
SET @EndPos = @BeginPos + 4
SET @Len = (@EndPos - @BeginPos) + 1
MENTRE (@BeginPos > 0 e @EndPos > 0 e @Len > 0) iniziare
SET @DirtyText = roba (@DirtyText, @BeginPos, @Length, '&')
SET @BeginPos = CHARINDEX ('&', @DirtyText)
SET @EndPos = @BeginPos + 4
SET @Len = (@EndPos - @BeginPos) + 1
FINE
-Sostituire l'entità HTML < con la ' <' carattere
SET @BeginPos = CHARINDEX ('< ', @DirtyText)
SET @EndPos = @BeginPos + 3
SET @Len = (@EndPos - @BeginPos) + 1
MENTRE (@BeginPos > 0 e @EndPos > 0 e @Len > 0) iniziare
SET @DirtyText = roba (@DirtyText, @BeginPos, @Length, ' <')
SET @BeginPos = CHARINDEX ('< ', @DirtyText)
SET @EndPos = @BeginPos + 3
SET @Len = (@EndPos - @BeginPos) + 1
FINE
-Sostituire l'entità HTML > con il ' >' carattere
SET @BeginPos = CHARINDEX ('> ', @DirtyText)
SET @EndPos = @BeginPos + 3
SET @Len = (@EndPos - @BeginPos) + 1
MENTRE (@BeginPos > 0 e @EndPos > 0 e @Len > 0) iniziare
SET @DirtyText = roba (@DirtyText, @BeginPos, @Length, ' >')
SET @BeginPos = CHARINDEX ('> ', @DirtyText)
SET @EndPos = @BeginPos + 3
SET @Len = (@EndPos - @BeginPos) + 1
FINE
-Sostituire l'entità HTML & con il carattere '&'
SET @BeginPos = CHARINDEX ('&', @DirtyText)
SET @EndPos = @BeginPos + 4
SET @Len = (@EndPos - @BeginPos) + 1
MENTRE (@BeginPos > 0 e @EndPos > 0 e @Len > 0) iniziare
SET @DirtyText = roba (@DirtyText, @BeginPos, @Length, '&')
SET @BeginPos = CHARINDEX ('&', @DirtyText)
SET @EndPos = @BeginPos + 4
SET @Len = (@EndPos - @BeginPos) + 1
FINE
-Sostituire l'entità HTML con il ' ' carattere
SET @BeginPos = CHARINDEX (' ', @DirtyText)
SET @EndPos = @BeginPos + 5
SET @Len = (@EndPos - @BeginPos) + 1
MENTRE (@BeginPos > 0 e @EndPos > 0 e @Len > 0) iniziare
SET @DirtyText = roba (@DirtyText, @BeginPos, @Length, ' ')
SET @BeginPos = CHARINDEX (' ', @DirtyText)
SET @EndPos = @BeginPos + 5
SET @Len = (@EndPos - @BeginPos) + 1
FINE
-Sostituire i tag con una nuova riga
SET @BeginPos = CHARINDEX('', @DirtyText)
SET @EndPos = @BeginPos + 3
SET @Len = (@EndPos - @BeginPos) + 1
MENTRE (@BeginPos > 0 e @EndPos > 0 e @Len > 0) iniziare
SET @DirtyText = roba (@DirtyText, @BeginPos, @Length, char (13) + CHAR(10))
SET @BeginPos = CHARINDEX('', @DirtyText)
SET @EndPos = @BeginPos + 3
SET @Len = (@EndPos - @BeginPos) + 1
FINE
-Sostituire qualsiasi < br / > Tag con una nuova riga
SET @BeginPos = CHARINDEX ('< br/> ', @DirtyText)
SET @EndPos = @BeginPos + 4
SET @Len = (@EndPos - @BeginPos) + 1
MENTRE (@BeginPos > 0 e @EndPos > 0 e @Len > 0) iniziare
SET @DirtyText = roba (@DirtyText, @BeginPos, @Length, ' char (13) + CHAR(10)')
SET @BeginPos = CHARINDEX ('< br/> ', @DirtyText)
SET @EndPos = @BeginPos + 4
SET @Len = (@EndPos - @BeginPos) + 1
FINE
-Sostituire qualsiasi < br / > Tag con una nuova riga
SET @BeginPos = CHARINDEX ('< br/> ', @DirtyText)
SET @EndPos = @BeginPos + 5
SET @Len = (@EndPos - @BeginPos) + 1
MENTRE (@BeginPos > 0 e @EndPos > 0 e @Len > 0) iniziare
SET @DirtyText = roba (@DirtyText, @BeginPos, @Length, ' char (13) + CHAR(10)')
SET @BeginPos = CHARINDEX ('< br/> ', @DirtyText)
SET @EndPos = @BeginPos + 5
SET @Len = (@EndPos - @BeginPos) + 1
FINE
-Rimuovere nulla tra < qualunque > Tag
SET @BeginPos = CHARINDEX ('< ', @DirtyText)
SET @EndPos = CHARINDEX ('> ', @DirtyText, CHARINDEX ('< ', @DirtyText))
SET @Len = (@EndPos - @BeginPos) + 1
MENTRE (@BeginPos > 0 e @EndPos > 0 e @Len > 0) iniziare
SET @DirtyText = roba (@DirtyText, @BeginPos, @Length, ')
SET @BeginPos = CHARINDEX ('< ', @DirtyText)
SET @EndPos = 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 >< corpo > test </corpo >< / HTML >');