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
Release: 2024-11-18 06:40:02
Original
1049 people have browsed it

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;
Copy after login
Copy after login

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>
Copy after login

The following query would return the stripped text:

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

Output:

This is a bold text.
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template