Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strip all HTML tags, except allowed

Tags:

html

regex

php

tags

I've seen a lot of expressions to remove a specific tag (or many specified tags), and one to remove all but one specific tag, but I haven't found a way to remove all except many excluded (i.e. all except p, b, i, u, a, ul, ol, li) in PHP. I'm far from good with regex, so I'd need a hand. :) Thanks!

like image 545
Lazlo Avatar asked Jun 06 '11 01:06

Lazlo


People also ask

How do I strip a tag in HTML?

The strip_tags() function strips a string from HTML, XML, and PHP tags. Note: HTML comments are always stripped. This cannot be changed with the allow parameter. Note: This function is binary-safe.

Which function is used to remove all HTML?

Strip_tags() is a function that allows you to strip out all HTML and PHP tags from a given string (parameter one), however you can also use parameter two to specify a list of HTML tags you want.

How do I strip a string in HTML?

To strip out all the HTML tags from a string there are lots of procedures in JavaScript. In order to strip out tags we can use replace() function and can also use . textContent property, . innerText property from HTML DOM.

What does it mean to strip HTML?

stripHtml( html ) Changes the provided HTML string into a plain text string by converting <br> , <p> , and <div> to line breaks, stripping all other tags, and converting escaped characters into their display values.


2 Answers

you can do this by usingstrip_tags function

¶ strip_tags — Strip HTML and PHP tags from a string

 strip_tags($contant,'tag you want to allow'); 

like

  strip_tags($contant,'<code><p>'); 
like image 153
NullPoiиteя Avatar answered Sep 19 '22 23:09

NullPoiиteя


strip_tags() does exactly this.

like image 30
Rufinus Avatar answered Sep 20 '22 23:09

Rufinus