Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 2nd, 2008, 10:48 AM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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>';
Sane is offline   Reply With Quote
Old Jan 2nd, 2008, 12:04 PM   #2
mark
Newbie
 
Join Date: Mar 2006
Posts: 14
Rep Power: 0 mark is on a distinguished road
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
mark is offline   Reply With Quote
Old Jan 2nd, 2008, 12:12 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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.
Sane is offline   Reply With Quote
Old Jan 2nd, 2008, 4:04 PM   #4
null_ptr0
11 years old
 
Join Date: Nov 2007
Posts: 79
Rep Power: 1 null_ptr0 is on a distinguished road
Remove Post Please

Remove Post Please
__________________
iload_0 iconst_1 ishl or
iload_0 iconst_2 idiv or
iload_0 iconst_2 iconst_1 imul idiv
[1] & [2] use the smallest stack size

Last edited by null_ptr0; Jan 2nd, 2008 at 4:06 PM. Reason: REMOVE POST PLEASE
null_ptr0 is offline   Reply With Quote
Old Jan 2nd, 2008, 4:06 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Re: Best Way To Strip Certain Tags From A String

Quote:
Originally Posted by null_ptr0 View Post
str_replace(string, string)
No. Sorry. Good try? But no.
Sane is offline   Reply With Quote
Old Jan 2nd, 2008, 4:09 PM   #6
null_ptr0
11 years old
 
Join Date: Nov 2007
Posts: 79
Rep Power: 1 null_ptr0 is on a distinguished road
Re: Best Way To Strip Certain Tags From A String

ereg_replace
preg_replace
RegEx - use them.
__________________
iload_0 iconst_1 ishl or
iload_0 iconst_2 idiv or
iload_0 iconst_2 iconst_1 imul idiv
[1] & [2] use the smallest stack size
null_ptr0 is offline   Reply With Quote
Old Jan 2nd, 2008, 4:34 PM   #7
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Re: Best Way To Strip Certain Tags From A String

Yes, so I can do something like:

php Syntax (Toggle Plain Text)
  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.
Sane is offline   Reply With Quote
Old Jan 2nd, 2008, 4:36 PM   #8
null_ptr0
11 years old
 
Join Date: Nov 2007
Posts: 79
Rep Power: 1 null_ptr0 is on a distinguished road
Re: Best Way To Strip Certain Tags From A String

Regex aren't tested?
__________________
iload_0 iconst_1 ishl or
iload_0 iconst_2 idiv or
iload_0 iconst_2 iconst_1 imul idiv
[1] & [2] use the smallest stack size
null_ptr0 is offline   Reply With Quote
Old Jan 2nd, 2008, 4:47 PM   #9
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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 4:58 PM.
Sane is offline   Reply With Quote
Old Jan 2nd, 2008, 7:59 PM   #10
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 403
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
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
grimpirate is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
An Attempt at a DBMS grimpirate PHP 8 Apr 17th, 2007 1:01 PM
Throwing an exception when using string constructor csrocker101 C# 3 Apr 8th, 2007 2:04 PM
Help with breaking apart a string csrocker101 C# 6 Apr 6th, 2007 7:50 AM
Function Parameters grimpirate PHP 10 Mar 14th, 2007 6:55 PM
Problems with String to MD5 Conversion emdiesse Visual Basic .NET 0 Feb 2nd, 2006 10:25 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:46 PM.

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