Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   Best Way To Strip Certain Tags From A String (http://www.programmingforums.org/showthread.php?t=14852)

Sane Jan 2nd, 2008 10:48 AM

Best Way To Strip Certain Tags From A String
 
I have a string handled by PHP. What's the best way to strip all "font" tags from that string? For example, I want $new_mystr to be obtained from $mystr.

E.G. Input
:

$mystr = '<div class="content"><p>Hello to all. <font face="Calibri"><font size="3">Goodbye</font></font></p></div>';

E.G. Output
:

$new_mystr = '<div class="content"><p>Hello to all. Goodbye</p></div>';

mark Jan 2nd, 2008 12:04 PM

Re: Best Way To Strip Certain Tags From A String
 
what about strip_tags function

http://us.php.net/manual/en/function.strip-tags.php

Sane Jan 2nd, 2008 12:12 PM

Re: Best Way To Strip Certain Tags From A String
 
Great!

Thank-you.

But I can only use this temporarily, because it's not exactly what I need. I want to specify tags for inclusion. Not for exclusion.

So currently I can go:

:

strip_tags($text, '<p><div>');

However, that's specifying tags to exclude. Whereas I want something like this:

:

strip_tags_only($text, '<font>');

To specify the only tags included in the stripping.

null_ptr0 Jan 2nd, 2008 4:04 PM

Remove Post Please
 
Remove Post Please

Sane Jan 2nd, 2008 4:06 PM

Re: Best Way To Strip Certain Tags From A String
 
Quote:

Originally Posted by null_ptr0 (Post 138982)
str_replace(string, string)

No. Sorry. Good try? But no. :yawn:

null_ptr0 Jan 2nd, 2008 4:09 PM

Re: Best Way To Strip Certain Tags From A String
 
ereg_replace
preg_replace
RegEx - use them.

Sane Jan 2nd, 2008 4:34 PM

Re: Best Way To Strip Certain Tags From A String
 
Yes, so I can do something like:

:

  1. function strip_tag($text, $tag) {
  2.     return ereg_replace('</?'.$tag.'[^>]*>', '', $text);
  3. }
  4.  
  5. echo strip_tag($text, 'font');


I'd still like to see if there's something tried and tested though.

null_ptr0 Jan 2nd, 2008 4:36 PM

Re: Best Way To Strip Certain Tags From A String
 
Regex aren't tested?

Sane Jan 2nd, 2008 4:47 PM

Re: Best Way To Strip Certain Tags From A String
 
You misunderstand me.

There are always exceptions that standard library functions are built to handle. This simple regex statement is breakable.

For instance, if you gave my code:

:

echo strip_tag('<font>Blah', 'font');

It would output:

:

Blah

When I would prefer it to output:

:

<font>Blah

So the user can see that the original HTML was malformed.


Another example is if the tag is incomplete.
:

echo strip_tag('<font face="arial". Hello World!</font> This is some more text.', 'font');

You will get:
:

This is some more text.

Which is again, not satisfactory.

That's why it's best to leave it to a function which handles input with regard to the existing standards. Anyways, this shouldn't even need explanation.

At this point, from the lack of other replies, I'd say nothing else exists. I'll stick with my regex, which does work fine.

grimpirate Jan 2nd, 2008 7:59 PM

Re: Best Way To Strip Certain Tags From A String
 
I would suggest taking a look at some BBCode snippets as that is the most popular method apparently of stripping tags and creating your own "tags". It's what most of the forums use. My own forum has a function I created called parsePost (located in the _udf folder under parsePost.php). You can take a look at it as it only allows certain tags to be used and then parses out the remainder. However, the function is rather long and bloated, it needs to be refined and it also doesn't check for flaws where the tags haven't been closed. As I recall it explodes about the < character and then analyzes the initial portion of each line to determine if it is indeed an HTML tag or just a less than or some other tag I've no interested in allowing to be rendered.


All times are GMT -5. The time now is 12:45 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC