MySQL itself does not have built-in functions to remove HTML code, but in some cases it has to extract some plain text from the database hierarchy that removes the HTML code.
After Google, found the following two functions, tested, are available.
SQL code
SET GLOBAL Log_bin_trust_function_creators=1;
DROP FUNCTION IF EXISTS fnstriptags;
DELIMITER |
CREATE FUNCTION fnstriptags (Dirty varchar (4000))
RETURNS varchar (4000)
Deterministic
BEGIN
DECLARE IStart, iend, Ilength int;
While Locate (' < ', Dirty) > 0 and Locate (' > ', Dirty, Locate (' < ', Dirty)) > 0 Do
BEGIN
SET IStart = Locate (' < ', Dirty), iend = Locate (' > ', Dirty, Locate (' < ', Dirty));
SET ilength = (iend-istart) + 1;
IF ilength > 0 THEN
BEGIN
SET Dirty = Insert (Dirty, IStart, Ilength, ");
End;
End IF;
End;
End while;
return Dirty;
End;
|
DELIMITER;
SELECT fnstriptags (' This
Function 2:
CREATE FUNCTION ' strip_tags ' ($str text) RETURNS text
begin
DECLARE $start, $end INT DEFAULT 1;
loop
& nbsp; 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;
end;
Select Strip_tags (' ; Span>hel<b>lo <a href= "World" >wo<>rld</a> <<x>again<. ');
Add: If you are a PHP programmer do not need the above method, directly have the current code
<?php
$text = ' <p>test paragraph.</p><!--Comment--> <a href= ' #fragment ' >other text</a> ';
echo Strip_tags ($text);
echo "";
Allow <p> and <a>
Echo strip_tags ($text, ' <p><a> ');
?>
The above routines will output:
Test paragraph. Other text
<p>test paragraph.</p> <a href= "#fragment" >other text</a>