The method to remove HTML tags in PHP is: 1. Create a PHP sample file; 2. Create a variable $html_string to store characters with html tags; 3. Use the "strip_tags($html_string)" syntax to All HTML tags and their contents are deleted from the string; 4. Output the deleted string through echo.
Operating system for this tutorial: Windows 10 system, php8.1.3 version, Dell G3 computer.
Use the "strip_tags()" function in PHP to remove HTML tags
$html_string variable contains the string of HTML tags to be deleted, then the following is a basic usage example:
$html_string = "<p>This is some <strong>bold</strong> text and </p><a href='#'>a link</a>"; $stripped_string = strip_tags($html_string); echo $stripped_string;
The output result is:
This is some bold text and a link
In the above code, a string $html_string containing HTML tags is first defined. Next, use the strip_tags() function to remove all HTML tags and their contents from this string, and store the result into the $stripped_string variable. Finally, the value of $stripped_string is output, which is the string with the HTML markup removed.
If you need to retain certain tags, you can specify the second parameter in the strip_tags() function, for example:
$html_string = "<p>This is some <strong>bold</strong> text and </p><a href='#'>a link</a>"; $stripped_string = strip_tags($html_string, '<p><a>'); echo $stripped_string;
Here you specify to retain the
and tags, $ stripped_string will only contain the content inside the tag.
The above is the detailed content of How to remove html tags in php code. For more information, please follow other related articles on the PHP Chinese website!