Tecnología

Inicio

Cómo convertir de HTML a texto en SQL

Cómo convertir de HTML a texto en SQL


Convertir el HTML al texto en SQL se puede hacer de manera eficiente con una función definida por el usuario. Una función definida por el usuario acepta parámetros, realiza una acción, tales como análisis de HTML, y devuelve el resultado como un valor. La función puede ejecutarse desde cualquier sentencia SQL o un lenguaje de programación externo.

Instrucciones

1 Conectarse a la base de datos, y crear un nuevo archivo de SQL.

2 Introduzca el siguiente código SQL:

CREATE FUNCTION [dbo]. [CleanHTML]

(

@DirtyText VARCHAR (MAX)

)

DEVOLUCIONES VARCHAR (MAX)

COMO

EMPEZAR

DECLARE @BeginPos int

DECLARE @EndPos int

DECLARE @Len int

- Sustituir la entidad HTML y con el carácter '&' (esto debe hacerse en primer lugar, como se

- '&' Podría ser el doble codificados como '&')

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

@EndPos SET = 4 + @BeginPos

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

MIENTRAS (@BeginPos> 0 y @EndPos> 0 y @Len> 0) COMIENZAN

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

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

@EndPos SET = 4 + @BeginPos

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

FIN

- Sustituir la entidad HTML <con el carácter '<'

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

@EndPos SET = 3 + @BeginPos

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

MIENTRAS (@BeginPos> 0 y @EndPos> 0 y @Len> 0) COMIENZAN

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

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

@EndPos SET = 3 + @BeginPos

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

FIN

- Sustituir la entidad HTML> con el carácter '>'

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

@EndPos SET = 3 + @BeginPos

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

MIENTRAS (@BeginPos> 0 y @EndPos> 0 y @Len> 0) COMIENZAN

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

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

@EndPos SET = 3 + @BeginPos

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

FIN

- Sustituir la entidad HTML y con el carácter '&'

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

@EndPos SET = 4 + @BeginPos

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

MIENTRAS (@BeginPos> 0 y @EndPos> 0 y @Len> 0) COMIENZAN

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

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

@EndPos SET = 4 + @BeginPos

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

FIN

- Sustituir la entidad HTML con el carácter ""

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

@EndPos SET = + 5 @BeginPos

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

MIENTRAS (@BeginPos> 0 y @EndPos> 0 y @Len> 0) COMIENZAN

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

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

@EndPos SET = + 5 @BeginPos

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

FIN

- Reemplace cualquier etiqueta con un salto de línea

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

@EndPos SET = 3 + @BeginPos

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

MIENTRAS (@BeginPos> 0 y @EndPos> 0 y @Len> 0) COMIENZAN

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

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

@EndPos SET = 3 + @BeginPos

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

FIN

- Reemplazar las etiquetas <br/> con un salto de línea

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

@EndPos SET = 4 + @BeginPos

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

MIENTRAS (@BeginPos> 0 y @EndPos> 0 y @Len> 0) COMIENZAN

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

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

@EndPos SET = 4 + @BeginPos

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

FIN

- Reemplazar las etiquetas <br /> con un salto de línea

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

@EndPos SET = + 5 @BeginPos

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

MIENTRAS (@BeginPos> 0 y @EndPos> 0 y @Len> 0) COMIENZAN

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

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

@EndPos SET = + 5 @BeginPos

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

FIN

- Eliminar cualquier cosa entre <> cualesquiera que sean las etiquetas

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

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

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

MIENTRAS (@BeginPos> 0 y @EndPos> 0 y @Len> 0) COMIENZAN

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

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

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

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

FIN

RETORNO LTRIM (RTRIM (@DirtyText))

FIN

3 Compilar la función SQL.

4 Ejecutar la función, y verifique que devuelve los resultados deseados. Por ejemplo:

Seleccione de dbo.CleanHTML ( '<HTML> <BODY> test </ BODY> </ HTML>');