Home > Database > Mysql Tutorial > How to Remove HTML Tags from MySQL Data Efficiently?

How to Remove HTML Tags from MySQL Data Efficiently?

Mary-Kate Olsen
Release: 2024-11-17 19:10:02
Original
739 people have browsed it

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

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

Query Result:

| title |
|---|---|
| This is an example title |
| Another example link |
Copy after login

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!

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