Home >Backend Development >PHP Tutorial >How Can I Remove HTML Tags from a PHP String and Trim the Result?

How Can I Remove HTML Tags from a PHP String and Trim the Result?

Susan Sarandon
Susan SarandonOriginal
2024-12-22 07:47:58263browse

How Can I Remove HTML Tags from a PHP String and Trim the Result?

Removing HTML Tags from PHP String

In PHP, eliminating HTML tags is necessary to ensure the desired text rendering, especially when dealing with user-generated content. A common scenario is displaying a trimmed version of a database entry containing HTML markup.

To remove HTML tags, the strip_tags() function can be employed. This function effectively strips all tags from a given string, leaving only the raw text. By utilizing strip_tags(), we can elegantly remove all HTML code and subsequently display the desired number of characters from the database entry.

Here's an example:

$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text); // Output: Test paragraph. Other text

To trim a database entry and display only the first 110 characters without HTML tags:

echo substr(strip_tags($row_get_Business['business_description']), 0, 110) . "...";

By employing strip_tags(), we can efficiently cleanse text of HTML tags, ensuring the display of clean and relevant content.

The above is the detailed content of How Can I Remove HTML Tags from a PHP String and Trim the Result?. 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