Home >Database >Mysql Tutorial >How to Effectively Remove HTML Tags from Database Records for Accurate Search Results?
When extracting specific information from a database table containing HTML-formatted text, it's essential to remove the HTML tags for accurate results. While a simple LIKE query like "WHERE column_name LIKE '%mytext%'" may retrieve all rows containing 'mytext', it may also include rows where 'mytext' appears within HTML tags.
To address this issue, a more effective approach involves creating a custom MySQL function that specifically removes HTML tags from a given string. Here's the code for such a function:
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 ;
By using this function, the query becomes:
SELECT * from table WHERE fnStripTags(column_name) LIKE '%mytext%'
Now, the query will only return rows where 'mytext' appears within the content itself, excluding any instances within HTML tags. This provides more accurate and relevant results, ensuring that only the desired data is retrieved.
The above is the detailed content of How to Effectively Remove HTML Tags from Database Records for Accurate Search Results?. For more information, please follow other related articles on the PHP Chinese website!