Re: Keep HTML tags, but strip attributes
| From: | Red Wingate | Date: | Sat, 03 Apr 2004 23:10:35 +0000 |
| Subject: | Re: Keep HTML tags, but strip attributes | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-182265@lists.php.net to get a copy of this message | ||
something like this might work (written on the fly):
$var = preg_replace( "/<\s*([a-z]+)([^>]*)>/i" , "<//1>" , $var );
special action on the closing tags is not necc. as they don't contain
any attributes.
-- red
Matt Palermo wrote:
Okay, I have it all set to remove ALL tags and their attributes that I don't want. Now I just have to strip the attributes from any remaining tags. Anyone know of something that will strip all attributes from any tag, but leave the tag in tact? So <p junk=junk> would be <p> and </p more junk> would be </p>. I just need this to work for any tag, no just <p>. Any suggestions for this? Thanks, Matt "Jochem Maas" <jochem@iamjochem.com> wrote in message news:406F35EE.3@iamjochem.com...Matt Palermo wrote:junk=junk>,I am building a system which allows users to post data. I want to allow them to use ONLY certain tags such as <p>, </p>, <b>, </b>, <i>, </i>, etc... I want to allow them to use only these, and then strip out ALL attributes inside the tags. So if they input something like <pit would switch it to just <p>. Anyone know of a way this can be done?regular expressions, heres an example: <?php $input = 'this <div>is some</div> <u><b class="haxor">bad</b></u> HTML'; echo "{$input}\n"; $input = preg_replace('/<\/?[^pbiu\/][^>]*>/', '', $input); echo "{$input}\n"; $input = preg_replace('/<([pbiu])[^>]*>/', '<\1>', $input); echo "{$input}\n"; $input = str_replace('bad', 'good', $input); echo "{$input}\n"; ?> you might also think about stripping <script> tags etc. try taking a look at some forum code (e.g. phpbb.com) to see how they do it. no doubt that some real regexp wizard could perform the above replacements in a single regexp but hopefully it gives you an idea... if your not yet familiar with regexps then I strongly recommend you read the relevant part of the manual - they are very handy things indeed.Thanks, Matt Palermo http://sweetphp.com