mysql fonction pour supprimer les balises html
Par PlaceOweb le samedi, mai 8 2021, 18:00 - SQL - Lien permanent
J'ai eu le même besoins que How to remove html tags except <br> from mysql data?, Remove HTML tags from record et SQL - Remove all HTML tags in a string
DELIMITER mydelimiter
CREATE FUNCTION `strip_tags`($str text) RETURNS text DETERMINISTIC
BEGIN
DECLARE $start, $end INT DEFAULT 1;
LOOP
SET $start = LOCATE("<", $str, $start);
IF (!$start) THEN RETURN $str; END IF;
SET $end = LOCATE(">", $str, $start);
IF (!$end) THEN SET $end = $start; END IF;
SET $str = INSERT($str, $start, $end - $start + 1, "");
END LOOP;
RETURN $str;
END;
mydelimiter
DELIMITER ;
select strip_tags('<span>hel<b>lo <balise/> <a href="world">wo<>rld</a> <<x>again<.');
+--------------------------------------------------------------------------------+
| strip_tags('<span>hel<b>lo <balise/> <a href="world">wo<>rld</a> <<x>again<.') |
+--------------------------------------------------------------------------------+
| hello world again. |
+--------------------------------------------------------------------------------+