Home >Backend Development >PHP Tutorial >How Can I Remove HTML Tags and Truncate Text in PHP?

How Can I Remove HTML Tags and Truncate Text in PHP?

Barbara Streisand
Barbara StreisandOriginal
2024-12-02 21:18:12818browse

How Can I Remove HTML Tags and Truncate Text in PHP?

Stripping HTML Tags from PHP Strings

Removing HTML tags from a PHP string is essential for displaying data from databases or user input without formatting interference. To achieve this, PHP offers the strip_tags() function.

Example:

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

In your case, you can strip out all HTML tags from the database entry and display the first 110 characters as follows:

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

This code will first remove all HTML tags using strip_tags(), then use substr() to display the specified number of characters from the resulting string.

The above is the detailed content of How Can I Remove HTML Tags and Truncate Text in PHP?. 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