How to Remove HTML Tags from Nsstring in Iphone

Remove HTML Tags from an NSString on the iPhone

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.

Remove HTML tags from NSString

You can tweak as

First replce <br/> with *br/* then replace all html tags and then again replace *br/* to <br/>.

Or, you can use conditional replacing .. if read tag is br continue else replace.

Remove html tags and display in UILabel Objective c

Use below UILabel formatting control which provides Rich text formatting based on HTML-like markups for iOS.
RTLabel

Remove broken HTML tags from NSString

try this code

- (NSString *)removeIncompleteHTMLTagInString:(NSString *)HTMLString {
NSArray *subStringByOpenTabs = [HTMLString componentsSeparatedByString:@"<"];
NSArray *subStringByCloseTabs = [HTMLString componentsSeparatedByString:@">"];
if (subStringByOpenTabs.count > subStringByCloseTabs.count) {
return [HTMLString substringToIndex:(HTMLString.length - ((NSString *)[subStringByOpenTabs lastObject]).length) -1];
}
else {
return HTMLString;
}
}

test:

NSLog(@"%@",[self removeIncompleteHTMLTagInString:@"This is some <xx> broken html tag<b"]);

output is: "This is some <xx> broken html tag"

How can I remove html tags from a string?

You can remove html tag from string by using NSAttributedString.

Please find the below code :

let htmlString = "<p style=\"text-align: right;\"> text and text"

do {
let encodedData = htmlString.dataUsingEncoding(NSUTF8StringEncoding)!
let attributedOptions : [String: AnyObject] = [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding
]
let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)

print("final strings :",attributedString.string)

} catch {
fatalError("Unhandled error: \(error)")
}

Hope it works for you!!!

You can also create String extension for reusability:

extension String {
init(htmlString: String) {
do {
let encodedData = htmlString.dataUsingEncoding(NSUTF8StringEncoding)!
let attributedOptions : [String: AnyObject] = [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding
]
let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
self.init(attributedString.string)
} catch {
fatalError("Unhandled error: \(error)")
}
}
}

Swift 3.0 - (Xcode 8.2) Update

extension String {

var normalizedHtmlString : String {

do {
if let encodedData = self.data(using: .utf8){
let attributedOptions : [String: AnyObject] = [
NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType as AnyObject,
NSCharacterEncodingDocumentAttribute: NSNumber(value: String.Encoding.utf8.rawValue)
]
let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
if let stringNormalized = String.init(attributedString.string){
return stringNormalized
}
}
}
catch {
assert(false, "Please check string")
//fatalError("Unhandled error: \(error)")
}
return self
}
}

And call the htmlString method :

let yourHtmlString = "<p style=\"text-align: right;\"> text and text"
let decodedString = String(htmlString:yourHtmlString)

How to remove all tags from html string?


extension String{
var htmlConvertedString : String{
let string = self.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
return string
}}

try this

and call like this let val = str.htmlConvertedString

print(val)



Related Topics



Leave a reply



Submit