Home > Backend Development > PHP Tutorial > How Can I Safely Display a Truncated Business Description from a Database Containing HTML Tags in PHP?

How Can I Safely Display a Truncated Business Description from a Database Containing HTML Tags in PHP?

Mary-Kate Olsen
Release: 2024-12-07 01:40:13
Original
350 people have browsed it

How Can I Safely Display a Truncated Business Description from a Database Containing HTML Tags in PHP?

Remove HTML Tags from PHP String

When displaying database entries, it's important to handle HTML content within the string. In one such instance, you want to display only the first 110 characters of a business description from a database, but the entry contains HTML code entered by the client.

As seen below, the HTML code breaks the display:

<?php echo substr($row_get_Business['business_description'],0,110) . "..."; ?>
Copy after login

To resolve this issue, you need to remove all HTML tags from the string. The strip_tags() function helps you achieve this.

$cleaned_text = strip_tags($text);
Copy after login

The strip_tags() function removes all HTML tags from the string, leaving you with the plain text. You can then use the function as follows to display the first 110 characters of the business description, excluding HTML tags:

<?php echo substr(strip_tags($row_get_Business['business_description']),0,110) . "..."; ?>
Copy after login

This will correctly display the first 110 characters of the business description without any HTML code.

The above is the detailed content of How Can I Safely Display a Truncated Business Description from a Database Containing HTML Tags 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