Home >Database >Mysql Tutorial >How to Strip HTML Tags from MySQL Data Using MySQL Queries?

How to Strip HTML Tags from MySQL Data Using MySQL Queries?

Linda Hamilton
Linda HamiltonOriginal
2024-11-18 06:40:021081browse

How to Strip HTML Tags from MySQL Data Using MySQL Queries?

Stripping HTML Tags from MySQL Data with MySQL Queries

Your database contains numerous records with HTML tags, and you wish to eliminate them without resorting to a time-consuming PHP script. This task can be accomplished effectively using MySQL queries.

MySQL Query Equivalent of PHP strip_tags

For MySQL versions 5.5 and above, XML functions offer a solution:

SELECT ExtractValue(field, '//text()') FROM table;

This query extracts the text content from the specified 'field' by parsing the HTML using XPaths. The '//text()' XPath selects all text nodes, excluding any HTML tags or attributes.

Example

Consider the HTML stored in the 'field' column of the 'table':

<p>This is a <b>bold</b> text.</p>

The following query would return the stripped text:

SELECT ExtractValue(field, '//text()') FROM table;

Output:

This is a bold text.

Reference

For further details on MySQL's XML functions:

https://dev.mysql.com/doc/refman/5.5/en/xml-functions.html

The above is the detailed content of How to Strip HTML Tags from MySQL Data Using MySQL Queries?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn