This string extension provides two main methods, stringByDecodingHTMLEntities and stringByStrippingHTMLTags.
import StringExtensionHTML var text = ""some html string with entities "" let decodedText = text.stringByDecodingHTMLEntities var text = "<span><p>Some text full of <i>unwanted</i> html tags</p></span> let decodedText = text.stringByStrippingHTMLTags
To run the example project, clone the repo, and run
pod install from the Example directory first.
StringExtensionHTML is available through CocoaPods. To install it, simply add the following line to your Podfile:
The latest pod works with Swift 4. For the Swift 3 version, add the following line:
pod 'StringExtensionHTML', '~> 0.2'
For the Swift 2 version, add the following line:
pod 'StringExtensionHTML', '~> 0.1'
Alternatively, copy StringExtensionHTML.swift and add that single file into your Swift project. If you use this method, you can omit the line "import StringExtensionHTML" at the top of your file.
Original solution and former adaptations credit to Michael Waterfall and Martin from the Stack Overflow thread How do I decode HTML entities in swift?. I upgraded to work with Swift 2.0 and made it available through CocoaPods.
Note: there is a Swift 2 version by Santiago in the original thread's comments. However, while that version will compile, it will not handle numeric character entities properly and also has some flaws which will cause it to crash anytime it comes to an entity it doesn't understand.
Adela Chang, [email protected]
StringExtensionHTML is available under the MIT license. See the LICENSE file for more info.