Tags
Asked 3 years ago
9 Jun 2021
Views 243
eclipse-learner

eclipse-learner posted

how to use strip_tags to REMOVE text between removed tags in php ?

how to use strip_tags to REMOVE text between removed tags in php ?
sachin

sachin
answered Apr 26 '23 00:00

By default, the strip_tags () function in PHP removes all HTML and PHP tags from a string, but preserves the text content between the tags. However, if you want to remove the text content between the removed tags as well, you can use a regular expression to achieve this.

Here's an example:



$string_with_tags_and_text = "<p>This is some <strong>text</strong> with <a href='#'>HTML</a> tags.</p>";
$stripped_string = preg_replace('/<[^>]*>/', '', $string_with_tags_and_text);
echo $stripped_string;

Output:




This is some text with HTML tags.

In this example, the preg_replace () function is used with a regular expression that matches any HTML or PHP tag (i.e., any sequence of characters between < and > characters), and replaces it with an empty string. As a result, both the tags and the text content between them are removed from the input string.

Note that this approach may not work for all cases, and may have some limitations. For example, it may not be able to handle complex or nested HTML structures, and may not be able to preserve certain formatting or layout information. It is generally recommended to use this approach only if you specifically need to remove both the tags and the text content between them, and to use other methods (such as the strip_tags () function without the second parameter, or a dedicated HTML parser library) for more general HTML processing needs.
Post Answer