How to remove html tags from data in php?



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

Post New Answer

More PHP Interview Questions

How does php serialize work?

1 Answers  


How to include a file code in different files in php?

1 Answers  


What is php routing?

1 Answers  


What is the purpose of destructor ?

2 Answers  


What are the characteristics of php?

1 Answers  


Differentiate between php5 and php7?

1 Answers  


What does $_cookie mean?

1 Answers  


How to calculate the difference between two dates using php?

0 Answers  


Which is better php or wordpress?

1 Answers  


What are some of the advanced features of php?

1 Answers  


How can we get second of the current time using date function?

2 Answers   FD, IBM,


What is difference between echo and print in php?

1 Answers  


Categories