Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove HTML Tags from an NSString on the iPhone

There are a couple of different ways to remove HTML tags from an NSString in Cocoa.

One way is to render the string into an NSAttributedString and then grab the rendered text.

Another way is to use NSXMLDocument's -objectByApplyingXSLTString method to apply an XSLT transform that does it.

Unfortunately, the iPhone doesn't support NSAttributedString or NSXMLDocument. There are too many edge cases and malformed HTML documents for me to feel comfortable using regex or NSScanner. Does anyone have a solution to this?

One suggestion has been to simply look for opening and closing tag characters, this method won't work except for very trivial cases.

For example these cases (from the Perl Cookbook chapter on the same subject) would break this method:

<IMG SRC = "foo.gif" ALT = "A > B">  <!-- <A comment> -->  <script>if (a<b && a>c)</script>  <![INCLUDE CDATA [ >>>>>>>>>>>> ]]> 
like image 377
lfalin Avatar asked Nov 10 '08 04:11

lfalin


People also ask

Is it possible to remove the HTML tags from data?

Strip_tags() is a function that allows you to strip out all HTML and PHP tags from a given string (parameter one), however you can also use parameter two to specify a list of HTML tags you want.

How do I remove text tags?

The HTML tags can be removed from a given string by using replaceAll() method of String class. We can remove the HTML tags from a given string by using a regular expression. After removing the HTML tags from a string, it will return a string as normal text.


1 Answers

A quick and "dirty" (removes everything between < and >) solution, works with iOS >= 3.2:

-(NSString *) stringByStrippingHTML {   NSRange r;   NSString *s = [[self copy] autorelease];   while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)     s = [s stringByReplacingCharactersInRange:r withString:@""];   return s; } 

I have this declared as a category os NSString.

like image 114
m.kocikowski Avatar answered Oct 06 '22 07:10

m.kocikowski