![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() ![]() |
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>'; |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Mar 2006
Posts: 19
Rep Power: 0
![]() |
Re: Best Way To Strip Certain Tags From A String
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() ![]() |
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. |
|
|
|
|
|
#4 |
|
12 years old
Join Date: Nov 2007
Posts: 105
Rep Power: 0
![]() |
Remove Post Please
Remove Post Please
Last edited by null_ptr0; Jan 2nd, 2008 at 5:06 PM. Reason: REMOVE POST PLEASE |
|
|
|
|
|
#5 |
|
Programming Guru
![]() ![]() |
Re: Best Way To Strip Certain Tags From A String
|
|
|
|
|
|
#6 |
|
12 years old
Join Date: Nov 2007
Posts: 105
Rep Power: 0
![]() |
Re: Best Way To Strip Certain Tags From A String
ereg_replace
preg_replace RegEx - use them. |
|
|
|
|
|
#7 |
|
Programming Guru
![]() ![]() |
Re: Best Way To Strip Certain Tags From A String
Yes, so I can do something like:
php Syntax (Toggle Plain Text)
I'd still like to see if there's something tried and tested though. |
|
|
|
|
|
#8 |
|
12 years old
Join Date: Nov 2007
Posts: 105
Rep Power: 0
![]() |
Re: Best Way To Strip Certain Tags From A String
Regex aren't tested?
|
|
|
|
|
|
#9 |
|
Programming Guru
![]() ![]() |
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. Last edited by Sane; Jan 2nd, 2008 at 5:58 PM. |
|
|
|
|
|
#10 |
|
King of Portal
|
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.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| An Attempt at a DBMS | grimpirate | PHP | 8 | Apr 17th, 2007 2:01 PM |
| Throwing an exception when using string constructor | csrocker101 | C# | 3 | Apr 8th, 2007 3:04 PM |
| Help with breaking apart a string | csrocker101 | C# | 6 | Apr 6th, 2007 8:50 AM |
| Function Parameters | grimpirate | PHP | 10 | Mar 14th, 2007 7:55 PM |
| Problems with String to MD5 Conversion | emdiesse | Visual Basic .NET | 0 | Feb 2nd, 2006 11:25 AM |