TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Trouble using regexps to explude HTML tags (http://www.talkphp.com/general/5358-trouble-using-regexps-explude-html-tags.html)

dc2000 03-17-2010 11:18 PM

Trouble using regexps to explude HTML tags
 
Hello:


My goal is to remove all HTML tags except the following:
// <b> and </b> = Bold;
// <i> and </i> = Italic;
// <u> and </u> = Underline;
// <ul> and </ul> = List;
// <ol> and </ol> = List;
// <li> and </li> = Bullet;
// <br> = Line Break;

So if I use the following line:
PHP Code:

$res preg_replace("/<\/?(b|i|u|ul|ol|li|br){1}>/i"""$raw); 

it removes the tags that I need and keeps the rest of them.

My question is how to "reverse" it?

delayedinsanity 03-18-2010 01:35 AM

You're looking for ^

On a side note, your pattern will exclude any of those tags that include attributes such as a style, class or id attribute. Not sure if that matters to you or not, just thought I would give you the heads up.

dc2000 03-18-2010 02:50 AM

Thanks. But where exactly do I put this ^?

And yes, again, all I need to let go through is the plain text and the following tags: <b>, </b>, <i>, </i>, <u>, </u>, <ul>, </ul>, <ol>, </ol>, <li>, </li>, <br>.

I'm sorry I'm not really good with regexs.

delayedinsanity 03-18-2010 04:59 AM

~</?[^b|i|u|ul|ol|li|br]+?>~

^ when used in a range means anything but ...

Sorry I'd go into more detail but I'm making dinner.

dc2000 03-18-2010 06:33 PM

Quote:

Originally Posted by delayedinsanity (Post 30260)
~</?[^b|i|u|ul|ol|li|br]+?>~

^ when used in a range means anything but ...

Sorry I'd go into more detail but I'm making dinner.

I tried your sample like this:
PHP Code:

$res preg_replace("/<\/?[^b|i|u|ul|ol|li|br]+?>/si"""$raw); 

and it still doesn't work. For example, it doesn't remove the following:

<a href="123">
or even
<le>

delayedinsanity 03-18-2010 08:04 PM

Dinner (especially steak dinner) trumps writing regular expressions.

It also trumps logical thought process. Such as using something more appropriate for the job at hand.

php Code:
// string strip_tags  (  string $str  [,  string $allowable_tags  ] )
$html = strip_tags( $html, '<b><i><u><ul><ol><li><br>' );

dc2000 03-18-2010 08:39 PM

Thanks! You're the man!


All times are GMT. The time now is 09:32 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0