How to remove html tags from data in php?
Answer / Ravi Kumar Arya
To remove HTML tags from a string in PHP, you can use the `strip_tags()` function. Here's an example:
```php
$html = '<div><h1>Hello World!</h1></div>';
$text = strip_tags($html);
echo $text; // Output: Hello World!
```
Note that this will remove all HTML tags, including self-closing tags like `<br />`. If you want to keep certain tags or strip only specific tags, you can use a regular expression to filter out the unwanted tags. Here's an example of stripping only `<script>` and `<style>` tags:
```php
function clean_html($str) {
$clean = preg_replace('/<script[^>]*?>.*?</script>/is', '', $str);
$clean = preg_replace('/<style[^>]*?>.*?</style>/is', '', $clean);
$clean = strip_tags($clean);
return $clean;
}
```
| Is This Answer Correct ? | 0 Yes | 0 No |
How does php serialize work?
How to include a file code in different files in php?
What is php routing?
What is the purpose of destructor ?
What are the characteristics of php?
Differentiate between php5 and php7?
What does $_cookie mean?
How to calculate the difference between two dates using php?
Which is better php or wordpress?
What are some of the advanced features of php?
How can we get second of the current time using date function?
What is difference between echo and print in php?