![]() |
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>'; |
Re: Best Way To Strip Certain Tags From A String
|
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. |
Remove Post Please
Remove Post Please
|
Re: Best Way To Strip Certain Tags From A String
Quote:
|
Re: Best Way To Strip Certain Tags From A String
ereg_replace
preg_replace RegEx - use them. |
Re: Best Way To Strip Certain Tags From A String
Yes, so I can do something like:
:
I'd still like to see if there's something tried and tested though. |
Re: Best Way To Strip Certain Tags From A String
Regex aren't tested?
|
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: :
BlahWhen I would prefer it to output: :
<font>BlahSo 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. |
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