Home >Database >Mysql Tutorial >How to Remove HTML Tags from MySQL Data Efficiently?
MySQL Equivalent to PHP strip_tags for Removing HTML Tags
To remove HTML tags from a large database containing records with elements, a PHP script using strip_tags followed by a database update is commonly employed. However, this approach can be time-consuming. Fortunately, MySQL offers a more efficient solution using XML functions introduced in version 5.5.
MySQL Query:
SELECT ExtractValue(field, '//text()') FROM table;
Explanation:
The ExtractValue() function extracts the text content from an XML document, effectively removing any markup tags. The '//text()' argument selects all text nodes in the XML document, which correspond to the content within the HTML tags.
Example:
Consider the following database:
| id | title | |---|---| | 1 | This is an example <h1>title</h1> | | 2 | Another example <a href="#">link</a> |
Query Result:
| title | |---|---| | This is an example title | | Another example link |
Reference:
https://dev.mysql.com/doc/refman/5.5/en/xml-functions.html
The above is the detailed content of How to Remove HTML Tags from MySQL Data Efficiently?. For more information, please follow other related articles on the PHP Chinese website!