Maison  >  Article  >  base de données  >  Comment décoder des entités HTML dans MySQL ?

Comment décoder des entités HTML dans MySQL ?

Linda Hamilton
Linda Hamiltonoriginal
2024-10-25 11:11:02803parcourir

How to Decode HTML Entities in MySQL?

Décodage des entités HTML dans MySQL

Les entités HTML sont des caractères spéciaux utilisés pour représenter divers symboles et caractères en HTML. Par exemple, le caractère " représente un guillemet, et < représente un signe inférieur à.

Si vous disposez de données texte contenant des entités HTML, vous souhaiterez peut-être les décoder afin que le texte s'affiche correctement. MySQL n'a pas de fonction intégrée pour décoder les entités HTML, mais vous pouvez créer une fonction définie par l'utilisateur (UDF) pour ce faire.

Voici un exemple d'UDF que vous pouvez utiliser pour décoder du HTML entités :

CREATE FUNCTION HTML_UnEncode(X VARCHAR(255)) RETURNS VARCHAR(255) CHARSET latin1 DETERMINISTIC
BEGIN

DECLARE TextString VARCHAR(255) ;
SET TextString = X ;

#quotation mark
IF INSTR( X , '&quot;' ) 
THEN SET TextString = REPLACE(TextString, '&quot;','"') ; 
END IF ;

#apostrophe 
IF INSTR( X , '&apos;' ) 
THEN SET TextString = REPLACE(TextString, '&apos;','"') ; 
END IF ;

#ampersand
IF INSTR( X , '&amp;' ) 
THEN SET TextString = REPLACE(TextString, '&amp;','&') ; 
END IF ;

#less-than 
IF INSTR( X , '&lt;' ) 
THEN SET TextString = REPLACE(TextString, '&lt;','<') ; 
END IF ;

#greater-than 
IF INSTR( X , '&gt;' ) 
THEN SET TextString = REPLACE(TextString, '&gt;','>') ; 
END IF ;

#non-breaking space
IF INSTR( X , '&nbsp;' ) 
THEN SET TextString = REPLACE(TextString, '&nbsp;',' ') ; 
END IF ;

#inverted exclamation mark
IF INSTR( X , '&iexcl;' ) 
THEN SET TextString = REPLACE(TextString, '&iexcl;','¡') ; 
END IF ;

#cent
IF INSTR( X , '&cent;' ) 
THEN SET TextString = REPLACE(TextString, '&cent;','¢') ; 
END IF ;

#pound
IF INSTR( X , '&pound;' ) 
THEN SET TextString = REPLACE(TextString, '&pound;','£') ; 
END IF ;

#currency
IF INSTR( X , '&curren;' ) 
THEN SET TextString = REPLACE(TextString, '&curren;','¤') ; 
END IF ;

#yen
IF INSTR( X , '&yen;' ) 
THEN SET TextString = REPLACE(TextString, '&yen;','¥') ; 
END IF ;

#broken vertical bar
IF INSTR( X , '&brvbar;' ) 
THEN SET TextString = REPLACE(TextString, '&brvbar;','¦') ; 
END IF ;

#section
IF INSTR( X , '&sect;' ) 
THEN SET TextString = REPLACE(TextString, '&sect;','§') ; 
END IF ;

#spacing diaeresis
IF INSTR( X , '&uml;' ) 

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn