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 [ >>>>>>>>>>>> ]]>
If you want to get the content without the html tags from the web page (HTML document) , then use this code inside the
UIWebViewDidfinishLoading
delegate method.this is work fine for me
Extending this more from m.kocikowski's and Dan J's answers with more explanation for newbies
1# First you have to create objective-c-categories to make the code useable in any class.
.h
.m
2# Then just import the .h file of the category class you've just created e.g.
3# Calling the Method.
result is NSString I want to strip the tags from.
Another one way:
Interface:
-(NSString *) stringByStrippingHTML:(NSString*)inputString;
Implementation
Realization
cell.exampleClass.text = [self stringByStrippingHTML:[exampleJSONParsingArray valueForKey: @"key"]];
or simple
NSString *myClearStr = [self stringByStrippingHTML:rudeStr];
Here's a blog post that discusses a couple of libraries available for stripping HTML http://sugarmaplesoftware.com/25/strip-html-tags/ Note the comments where others solutions are offered.