connettersi al database , e . creare un nuovo file SQL
2
Inserisci il seguente SQL :
CREATE FUNCTION [ dbo ] [ CleanHTML ]
(
@ . DirtyText varchar ( MAX ) per
) per
RETURNS varchar ( MAX ) per
AS
BEGIN
DECLARE @ BeginPos int
DECLARE @ endpos int
DECLARE @ Len int
- Sostituire l'entità HTML e con il carattere '&' ( questo deve essere fatto prima , come
- '&' potrebbe essere doppio codificato come '&' ) per
SET @ BeginPos = CHARINDEX ( '&' , @ DirtyText ) per
SET @ endpos = @ BeginPos + 4 < br
SET > @ Len = ( @ endpos - @ BeginPos ) + 1
WHILE ( @ BeginPos > 0 e @ endpos > 0 e @ Len > 0 ) BEGIN
SET @ DirtyText = STUFF ( @ DirtyText , BeginPos @ , @ Length , '&' ) per
SET @ BeginPos = CHARINDEX ( '&' , @ DirtyText ) per
SET @ endpos = @ BeginPos + 4
SET @ Len = ( @ endpos - @ BeginPos ) + 1
END
- Sostituire il codice HTML entità < con il carattere '<' < br
> @ BeginPos = CHARINDEX ( '<' , @ DirtyText ) per
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 , ' < ; ' ) per
SET @ BeginPos = CHARINDEX ( ' <' , @ DirtyText ) per
SET @ endpos = @ BeginPos + 3
SET @ Len = ( @ endpos - @ BeginPos ) + 1
END
- Sostituire il codice HTML entità > con il '>' carattere
SET @ BeginPos = CHARINDEX ( '>' , @ DirtyText ) per
SET @ endpos = @ BeginPos + 3
SET @ Len = ( @ endpos - @ BeginPos ) + 1
WHILE ( @ BeginPos > 0 E @ endpos > 0 e @ Len > 0 ) BEGIN
SET @ DirtyText = STUFF ( @ DirtyText , BeginPos @ , @ Lunghezza , '>' ) per
SET @ BeginPos = CHARINDEX ( '>' , @ DirtyText ) per
SET @ endpos = @ BeginPos + 3
SET @ Len = ( @ endpos - @ BeginPos ) + 1
END
- Sostituire l'entità HTML e con il carattere '&'
SET @ BeginPos = CHARINDEX ( '&' , @ DirtyText ) per
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 , '&' ) per
SET @ BeginPos = CHARINDEX ( '&' , @ DirtyText ) per
SET @ endpos = @ BeginPos + 4
SET @ Len = ( @ endpos - @ BeginPos ) + 1
END
- Sostituire l'entità HTML con il carattere ' ' < br
> @ BeginPos = CHARINDEX ('' , @ DirtyText ) per
SET @ endpos = @ BeginPos + 5
SET @ Len = ( @ endpos - @ BeginPos ) + 1
WHILE ( @ BeginPos > 0 e @ endpos > 0 e @ Len > 0 ) BEGIN
SET @ DirtyText = STUFF ( @ DirtyText , BeginPos @ , @ Lunghezza , ' ' ) per
SET @ BeginPos = CHARINDEX (' ' , @ DirtyText ) per
SET @ endpos = @ BeginPos + 5
SET @ Len = ( @ endpos - @ BeginPos ) + 1
END
- Sostituire i tag con un a capo
SET @ BeginPos = CHARINDEX ( '' , @ DirtyText ) per
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) ) per
SET @ BeginPos = CHARINDEX ( '' , @ DirtyText ) per
SET @ endpos = @ BeginPos + 3
SET @ Len = ( @ endpos - @ BeginPos ) + 1
END
- sostituire eventuali
tag con un a capo
SET @ BeginPos = CHARINDEX ( '
' , @ DirtyText ) per
SET @ endpos = @ BeginPos + 4
SET @ Len = ( @ endpos - @ BeginPos ) + 1
WHILE ( @ BeginPos > 0 e @ endpos > 0 e @ Len > 0 ) BEGIN
SET @ DirtyText = STUFF ( @ DirtyText , BeginPos @ , @ Lunghezza , ' CHAR ( 13) + CHAR ( 10) ' ) per
SET @ BeginPos = CHARINDEX ( '
' , @ DirtyText ) per
SET @ endpos = @ BeginPos + 4
SET @ Len = ( @ endpos - @ BeginPos ) + 1
END
- sostituire i tag
con un a capo
SET @ BeginPos = CHARINDEX ( '
', @ DirtyText ) per
SET @ endpos = @ BeginPos + 5
SET @ Len = ( @ endpos - @ BeginPos ) + 1
WHILE ( @ BeginPos > 0 e @ endpos > 0 e @ Len > 0 ) BEGIN
SET @ DirtyText = STUFF ( @ DirtyText , BeginPos @ , @ Lunghezza , ' CHAR ( 13) + CHAR ( 10) ' ) per
SET @ BeginPos = CHARINDEX ( '
' , @ DirtyText ) per
SET @ endpos = @ BeginPos + 5
SET @ Len = ( @ endpos - @ BeginPos ) + 1
END
- rimuovere qualsiasi cosa tra i tag SET @ BeginPos = CHARINDEX ( '<' , @ DirtyText ) per SET @ endpos = CHARINDEX ( '>' , @ DirtyText , CHARINDEX ( '<' , @ DirtyText ) ) per SET @ Len = ( @ endpos - @ BeginPos ) + 1 WHILE ( @ BeginPos > 0 e @ endpos > 0 e @ Len > , 0 ) BEGIN SET @ DirtyText = STUFF ( @ DirtyText , BeginPos @ , @ Length , '' ) per SET @ BeginPos = CHARINDEX ( '<' , @ DirtyText ) < br SET > @ endpos = CHARINDEX ( '>' , @ DirtyText , CHARINDEX ( '<' , @ DirtyText ) ) per SET @ Len = ( @ endpos - @ BeginPos ) + 1 END RITORNO LTRIM ( RTRIM ( @ DirtyText ) ) per END Compilare la funzione SQL . Eseguire la funzione , e verificare che restituisce i risultati desiderati . Per esempio : Selezionare da dbo.CleanHTML ( ' prova
3
4
Informazioni correlate
Programmazione © www.354353.com