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
Release: 2024-12-02 21:18:12
Original
814 people have browsed it

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

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

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!

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