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) . "..."; ?>
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);
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) . "..."; ?>
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!